Buffalo 学习笔记- buffalo.js 源代码注释(一)
注: 如果转载 请注明
原文地址: http://blog.csdn.net/jianglike18/archive/2009/04/14/4073907.aspx
/*
* 申明 改代码的所有权归原作者,
本人只是给该代码加入自己理解的注释 ,如果你有任何疑问都可以
留言互相讨论
*
*/
// create Buffalo object with prototype.js package
var Buffalo = Class.create();
Buffalo.BOCLASS="_BUFFALO_OBJECT_CLASS_";
Buffalo.VERSION="2.0";
Buffalo.prototype = {
//创建 Buffalo 对象的时候会调用initialize方法
initialize: function(gateway, async, events, options) {
this.gateway = gateway;
this.transport = null;
if (typeof(async) == 'undefined') {
this.async = true;
} else {
this.async = async;
}
this.currentCallback = new Function();
this.setEvents(events);
this.queue = [];
this.requesting = false;
this.options = {timeout:30000};
//扩展 options 属性 使用了 prototype.js库的函数
Object.extend(this.options, options || {});
},
getGateway : function() { return this.gateway;},
//设置调用java服务的相关事项
setEvents: function(events) {
this.events = {
onLoading: Buffalo.Default.showLoading,
onFinish: new Function(),
onException: null,
onError: Buffalo.Default.showError,
onTimeout: new Function()
};
Object.extend(this.events, events || {});
},
//远程调用 URL : 远程的调用URL,
_remoteCall: function(url, buffaloCall, callback) {
this.requesting = true;
//创建 httpRequest请求
this.transport = XmlHttp.create();
try {
// 打开远程地址.指定 同步或者异步 和 POST方式
this.transport.open("POST", url, this.async);
//设置相关的请求头信息
this.transport.setRequestHeader("X-Buffalo-Version", Buffalo.VERSION);
this.transport.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
//发送数据
this.transport.send(buffaloCall.xml());
} catch (e) {
this.events.onError(this.transport);
this.events["onLoading"](false);
return;
}
this.requestTime = new Date();
// prototype.js 库的一定间隔时间上重复调用一个方法
this.timeoutHandle = new PeriodicalExecuter(this._timeoutChecker.bind(this), 0.5);
this.currentCallback = callback;
//异步调用
if (this.async) {
this.transport.onreadystatechange = this.onStateChange.bind(this);
this.events["onLoading"](true);
} else { //同步调用
this.response();
}
},
//判断是否超时
_timeoutChecker: function() {
if ((new Date() - this.requestTime) > this.options.timeout) {
this.events["onTimeout"]();
this.timeoutHandle.stop();
}
},
//
nextRemoteCall : function() {
if (this.queue.length <= 0) return ;
var command = this.queue.shift();
this._remoteCall(command.url, command.call, command.callback);
},
//进行远程调用(可以将N个远程调用的服务放入 queue 数组中 )
remoteCall: function(service, params, callback) {
var serviceMethodPair = this._splitServiceMethod(service);
var newUrl = this.gateway+"/buffalo/"+serviceMethodPair[0];
var call = new Buffalo.Call(serviceMethodPair[1]);
//将参数添加到 Buffalo.Call的
for (var i = 0; i < params.length; i++) {
call.addParameter(params[i]);
}
this.queue.push({url:newUrl, call: call, callback: callback});
if (!this.requesting) {
this.nextRemoteCall();
}
},
//分离出远程调用的服务类ID(如果BUFFALO是被Springl继承那么,那么应该在Spring的配置文件中有定义) 和 服务类的方法
_splitServiceMethod: function(service) {
var idx = service.lastIndexOf(".");
var serviceId = service.substring(0,idx);
var method = service.substring(idx+1,service.length);
return [serviceId, method];
},
//当服务器有反应的时候调用该函数
onStateChange: function(){
if (this.transport.readyState == 4) {
this.response();
}
},
//处理服务器的返回信息
response : function() {
this.timeoutHandle.stop();//停止超时判断函数的执行
this.events["onLoading"](false);//隐藏加载等待页面
if (this.transport.responseText && this.transport.status == '200') {
var reply = new Buffalo.Reply(this.transport);
if (reply.isFault()) {
if (this.events["onException"]) {
this.events["onException"](reply.getResult());
} else {
Buffalo.Default.showException(reply.getResult());
this.currentCallback(reply);
}
} else {
this.currentCallback(reply);
}
this.events["onFinish"](reply);
this.requesting = false;
this.nextRemoteCall();
} else {
this.events["onError"](this.transport);
this.requesting = false;
}
}
}
// 148~209 行 定义了 显示加载 出错 异常 超时 等相关状态的信息
Buffalo.Default = {
loadingPane: null,
errorPane: null,
exceptionPane: null,
//显示加载的等待页面 这个可以根据实际的情况进行修改
showLoading : function(state) {
this.loadingPane = $("buffalo_loading");
if (this.loadingPane == null) {
var el = document.createElement('DIV');
el.setAttribute("id","buffalo_loading");
el.style.cssText="display:none;font-family:Verdana;font-size:11px;border:1px solid #00CC00;background-color:#A4FFA4;padding:1px;position:absolute; right:1px; top:1px; width:110px; height:14px; z-index:10000";
el.innerHTML="buffalo loading... ";
document.body.appendChild(el);
this.loadingPane = el;
}
if (state) {
this.loadingPane.style.display="block";
this.loadingPane.style.top = document.body.scrollTop+1;
} else {
this.loadingPane.style.display="none";
}
},
showError: function(transport) {
this.errorPane = $("buffalo_error");
if (this.errorPane == null) {
var el = document.createElement('DIV');
el.setAttribute("id","buffalo_error");
el.style.cssText="font-size:11px;border:4px solid #FF3333;background-color:#fff;padding:4px;position:absolute;overflow:auto; right:10px; top:10px; width:500px; height:300px; z-index:1";
el.innerHTML="