Ajax.js
/**
* 定義 Ajax 對象, 靜態(tài)方法 getTransport 方法返回一個(gè) XMLHttp 對象
*/
var Ajax = {
getTransport: function() {
return Try.these(
function() {return new ActiveXObject(‘Msxml2.XMLHTTP‘)},
function() {return new ActiveXObject(‘Microsoft.XMLHTTP‘)},
function() {return new XMLHttpRequest()}
) || false;
},
emptyFunction: function() {}
}
/**
* 我以為此時(shí)的Ajax對象起到命名空間的作用。
* Ajax.Base 聲明為一個(gè)基礎對象類(lèi)型
* 注意 Ajax.Base 并沒(méi)有使用 Class.create() 的方式來(lái)創(chuàng )建,我想是因為作者并不希望 Ajax.Base 被庫使用者實(shí)例化。
* 作者在其他對象類(lèi)型的聲明中,將會(huì )繼承于它。
* 就好像 java 中的私有抽象類(lèi)
*/
Ajax.Base = function() {};
Ajax.Base.prototype = {
/**
* extend (見(jiàn)prototype.js中的定義) 的用法真是讓人耳目一新
* options 首先設置默認屬性,然后再 extend 參數對象,那么參數對象中也有同名的屬性,那么就覆蓋默認屬性值。
* 想想如果我寫(xiě)這樣的實(shí)現,應該類(lèi)似如下:
setOptions: function(options) {
this.options.methed = options.methed? options.methed : ‘post‘;
..........
}
我想很多時(shí)候,java 限制了 js 的創(chuàng )意。
*/
setOptions: function(options) {
this.options = {
method: ‘post‘,
asynchronous: true,
parameters: ‘‘
}.extend(options || {});
}
}
/**
* Ajax.Request 封裝 XmlHttp
*/
Ajax.Request = Class.create();
/**
* 定義四種事件(狀態(tài)), 參考
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/readystate_1.asp*/
Ajax.Request.Events =
[‘Uninitialized‘, ‘Loading‘, ‘Loaded‘, ‘Interactive‘, ‘Complete‘];
/**
*
*/
Ajax.Request.prototype = (new Ajax.Base()).extend({
initialize: function(url, options) {
this.transport = Ajax.getTransport();
this.setOptions(options);
try {
if (this.options.method == ‘get‘)
url += ‘?‘ + this.options.parameters + ‘&_=‘;
/**
* 此處好像強制使用了異步方式,而不是依照 this.options.asynchronous 的值
*/
this.transport.open(this.options.method, url, true);
/**
* 這里提供了 XmlHttp 傳輸過(guò)程中每個(gè)步驟的回調函數
*/
if (this.options.asynchronous) {
this.transport.onreadystatechange = this.onStateChange.bind(this);
setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
}
this.transport.setRequestHeader(‘X-Requested-With‘, ‘XMLHttpRequest‘);
this.transport.setRequestHeader(‘X-Prototype-Version‘, Prototype.Version);
if (this.options.method == ‘post‘) {
this.transport.setRequestHeader(‘Connection‘, ‘close‘);
this.transport.setRequestHeader(‘Content-type‘,
‘a(chǎn)pplication/x-www-form-urlencoded‘);
}
this.transport.send(this.options.method == ‘post‘ ?
this.options.parameters + ‘&_=‘ : null);
} catch (e) {
}
},
onStateChange: function() {
var readyState = this.transport.readyState;
/**
* 如果不是 Loading 狀態(tài),就調用回調函數
*/
if (readyState != 1)
this.respondToReadyState(this.transport.readyState);
},
/**
* 回調函數定義在 this.options 屬性中,比如:
var option = {
onLoaded : function(req) {...};
......
}
new Ajax.Request(url, option);
*/
respondToReadyState: function(readyState) {
var event = Ajax.Request.Events[readyState];
(this.options[‘on‘ + event] || Ajax.emptyFunction)(this.transport);
}
});
/**
* Ajax.Updater 用于綁定一個(gè)html元素與 XmlHttp調用的返回值。類(lèi)似與 buffalo 的 bind。
* 如果 options 中有 insertion(from dom.js) 對象的話(huà), insertion 能提供更多的插入控制。
*/
Ajax.Updater = Class.create();
Ajax.Updater.prototype = (new Ajax.Base()).extend({
initialize: function(container, url, options) {
this.container = $(container);
this.setOptions(options);
if (this.options.asynchronous) {
this.onComplete = this.options.onComplete;
this.options.onComplete = this.updateContent.bind(this);
}
this.request = new Ajax.Request(url, this.options);
if (!this.options.asynchronous)
this.updateContent();
},
updateContent: function() {
if (this.options.insertion) {
new this.options.insertion(this.container,
this.request.transport.responseText);
} else {
this.container.innerHTML = this.request.transport.responseText;
}
if (this.onComplete) {
setTimeout((function() {this.onComplete(this.request)}).bind(this), 10);
}
}
});
[Edit on 2005-07-06 17:46:10 By 澤欣(醒來(lái))]