2011/04/08

prototype.js extend Ajax.Queue

javascript
  1. var GUID = function () {
  2.     function S4() {
  3.         return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  4.     }
  5.     function guid() {
  6.         return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  7.     }
  8.     return guid();
  9. };


javascript
  1. Ajax.Queue = Class.create({
  2.     initialize: function () {
  3.         this.id = GUID();
  4.         this.active = false;
  5.         this.completed = true;
  6.         this.queue = [];
  7.         this.stopped = false;
  8.         Ajax.Queues.push(this);
  9.     },
  10.     add: function (url, options) {
  11.         this.queue.push({ "url": url, "options": options });
  12.         return this.queue.length - 1;
  13.     },
  14.     enqueue: function (url, options) {
  15.         var index = this.add(url, options);
  16.         if (!this.active) this.start();
  17.         return index;
  18.     },
  19.     clear: function () {
  20.         this.queue = [];
  21.     },
  22.     start: function () {
  23.         if (this.queue.length == 0) {
  24.             this.active = false;
  25.             this.completed = true;
  26.         }
  27.         else if (this.stopped) {
  28.             this.stopped = false;
  29.             this.clear();
  30.             this.active = false;
  31.             this.completed = true;
  32.         }
  33.         else {
  34.             this.active = true;
  35.             var params = this.queue.shift();
  36.             params.options = params.options || {};
  37.             var handlers = {
  38.                 "Success": function (transport) {
  39.                     if (typeof console == "object") console.log("success");
  40.                     this.start();
  41.                 },
  42.                 "Failure": function (transport) {
  43.                     if (typeof console == "object") console.log("failure");
  44.                     this.active = false;
  45.                 },
  46.                 "Complete": function (transport) {
  47.                     if (typeof console == "object") console.log("complete");
  48.                     this.active = false;
  49.                 },
  50.                 "Exception": function (transport, e) {
  51.                     if (typeof console == "object") console.log("exception");
  52.                     this.active = false;
  53.                     this.exception = e;
  54.                 }
  55.             };
  56.             var that = this;
  57.             Object.keys(handlers).each(function (event) {
  58.                 var eventName = "on" + event;
  59.                 var originalHandler = params.options[eventName];
  60.                 var handler = handlers[event];
  61.                 params.options[eventName] = (function () {
  62.                     if (Object.isFunction(originalHandler)) {
  63.                         originalHandler.apply(this, arguments);
  64.                     }
  65.                     handler.apply(this, arguments);
  66.                 }).bind(that);
  67.             });
  68.             this.completed = false;
  69.             new Ajax.Request(params.url, params.options);
  70.         }
  71.     },
  72.     stop: function () {
  73.         this.stopped = true;
  74.     }
  75. });
  76. Ajax.Queues = [];
  77. Ajax.Q = new Ajax.Queue();


usage
  1. Ajax.Q.enqueue("url", {
  2.     onSuccess: function (transport) {
  3.         try {
  4.             alert(transport.responseText);
  5.         } catch (e) { if(typeof(console) == "object") console.log(e) }
  6.     },
  7.     onFailure: function (ajax) {
  8.         if(typeof(console) == "object") console.log(e);
  9.     }
  10. });

沒有留言: