為 moo.ajax 增加狀態處理

上次介紹過 moo.fx這款輕巧靈活的 JSFramework。

其中的 moo.ajax 因為只能設定 onComplete 與 update Container 我覺得有點過於陽春了。
常用的 onLoading、404、500 等狀態處理在原始的 moo.ajax 並沒有附加上去,所以參考了Prototype原始版本的狀態處理方式替 moo.ajax 做了些修改。

使用的方式與原始的 moo.fx 差不多。

javascript · [高亮] · [原始]

  1. new ajax(url, {
  2.         postBody: params,
  3.         update: ‘commentlist’
  4.         onComplete: complete,
  5.         onLoading: loading,
  6.         on404: notFound,
  7.         on500: serveError,
  8.     });
  9. // 其後再定義每個狀態處理的函式就行了,如下
  10.     function loading(request) {
  11.         alert(‘loading…’);
  12.     }
new ajax(url, {
		postBody: params,
		update: \'commentlist\'
		onComplete: complete,
		onLoading: loading,
		on404: notFound,
		on500: serveError,
	});
// 其後再定義每個狀態處理的函式就行了,如下
	function loading(request) {
		alert(\'loading...\');
	}

修改過後的 moo.fx

javascript · [高亮] · [原始]

  1. //based on prototype’s ajax class
  2. //to be used with prototype.lite, moofx.mad4milk.net.
  3.  
  4. ajax = Class.create();
  5. ajax.event = [‘Uninitialized’, ‘Loading’, ‘Loaded’, ‘Interactive’, ‘Complete’];
  6. function emptyFunction() {}
  7. ajax.prototype = {
  8.     initialize: function(url, options){
  9.         this.transport = this.getTransport();
  10.         this.options = options;
  11.         this.postBody = options.postBody || ;
  12.         this.method = options.method || ‘post’;
  13.         this.update = $(options.update) || null;
  14.         this.request(url);
  15.     },
  16.  
  17.     request: function(url){
  18.         this.transport.open(this.method, url, true);
  19.         this.transport.onreadystatechange = this.onStateChange.bind(this);
  20.         if (this.method == ‘post’) {
  21.             this.transport.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’);
  22.             if (this.transport.overrideMimeType) this.transport.setRequestHeader(‘Connection’, ‘close’);
  23.         }
  24.         this.transport.send(this.postBody);
  25.     },
  26.  
  27.     onStateChange: function(){
  28.         var event = ajax.event[this.transport.readyState];
  29.         if (event == ‘Complete’) {
  30.             setTimeout(function(){(this.options[‘on’ + this.transport.status] || emptyFunction)(this.transport);}.bind(this), 10);
  31.             if (this.transport.status == 200 && this.update)
  32.                 setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
  33.         }
  34.         setTimeout(function(){(this.options[‘on’ + event] || emptyFunction)(this.transport);}.bind(this), 10);
  35.        
  36.         if (event == ‘Complete’)
  37.             this.transport.onreadystatechange = function(){};
  38.     },
  39.  
  40.     getTransport: function() {
  41.         if (window.ActiveXObject) return new ActiveXObject(‘Microsoft.XMLHTTP’);
  42.         else if (window.XMLHttpRequest) return new XMLHttpRequest();
  43.         else return false;
  44.     }
  45. };
//based on prototype\'s ajax class
//to be used with prototype.lite, moofx.mad4milk.net.

ajax = Class.create();
ajax.event = [\'Uninitialized\', \'Loading\', \'Loaded\', \'Interactive\', \'Complete\'];
function emptyFunction() {}
ajax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.options = options;
		this.postBody = options.postBody || \'\';
		this.method = options.method || \'post\';
		this.update = $(options.update) || null;
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == \'post\') {
			this.transport.setRequestHeader(\'Content-type\', \'application/x-www-form-urlencoded\');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader(\'Connection\', \'close\');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		var event = ajax.event[this.transport.readyState];
		if (event == \'Complete\') {
			setTimeout(function(){(this.options[\'on\' + this.transport.status] || emptyFunction)(this.transport);}.bind(this), 10);
			if (this.transport.status == 200 && this.update)
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
		}
		setTimeout(function(){(this.options[\'on\' + event] || emptyFunction)(this.transport);}.bind(this), 10);

		if (event == \'Complete\')
			this.transport.onreadystatechange = function(){};
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject(\'Microsoft.XMLHTTP\');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};

2 Responses to “為 moo.ajax 增加狀態處理”

  1. 兵敗如山倒 Says:

    這樣子好用多了!謝謝你^^

  2. Says:

    真的比較好

Leave a Comment