当前位置: 首页 > 工具软件 > AjaxPro > 使用案例 >

AjaxPro学习笔记

丁均
2023-12-01

//页面首次加载过程:
Utility.RegisterTypeForAjax :
将以下脚本注册:
<script type="text/javascript" src="/AjaxPro.Sample/ajaxpro/prototype.ashx"></script>
<script type="text/javascript" src="/AjaxPro.Sample/ajaxpro/core.ashx"></script>
<script type="text/javascript" src="/AjaxPro.Sample/ajaxpro/converter.ashx"></script>
<script type="text/javascript" src="/AjaxPro.Sample/ajaxpro/AjaxPro.Sample.Sample_1,AjaxPro.Sample.ashx"></script>
.net运行框架调用以下方法:
AjaxHandlerFactory.GetHandler
1.由于.ashx被注册为自定义处理,此方法被调用4次;
2.依据请求类型(get)和请求的目标进行分别处理,分别对应EmbeddedJavaScriptHandler(2次),ConverterJavaScriptHandler(1次),TypeJavaScriptHandler(1次)
3..net运行框架在AjaxHandlerFactory.GetHandler返回不同的IHttpHandler实现(EmbeddedJavaScriptHandler等)时调用该实现的以下方法:
EmbeddedJavaScriptHandler.ProcessRequest方法被调用:
EmbeddedJavaScriptHandler.ProcessRequest方法被调用:
ConverterJavaScriptHandler.ProcessRequest方法被调用:
TypeJavaScriptHandler.ProcessRequest方法被调用:
向客户端输出以下代码:
addNamespace("AjaxPro.Sample");
AjaxPro.Sample.Sample_1_class = Class.create();
AjaxPro.Sample.Sample_1_class.prototype = (new AjaxPro.AjaxClass()).extend(
{
              GetServerTime: function() 
              {
                            return this.invoke("GetServerTime", {}, this.GetServerTime.getArguments().slice(0));
              },
              AddTwo: function(firstInt, secondInt) 
              {
                            return this.invoke("AddTwo", {"firstInt":firstInt, "secondInt":secondInt}, this.AddTwo.getArguments().slice(2));
              },
              initialize: function() 
              {
                            this.url = '/AjaxPro.Sample/ajaxpro/AjaxPro.Sample.Sample_1,AjaxPro.Sample.ashx';
              }
});
AjaxPro.Sample.Sample_1 = new AjaxPro.Sample.Sample_1_class();
//分析prototype.js
var Class = {
              create: function() {
                            return function() {
                                                                      if(typeof this.initialize == "function")
                                                                      this.initialize.apply(this, arguments);
                                          }
                            }
}
AjaxPro.AjaxClass = Class.create();
AjaxPro.AjaxClass.prototype = {
              initialize: function(url) {
                            this.url = url;
              },
              invoke: function(method, args, e) {
                            if(e != null) {
                                          if(e.length != 6) for(;e.length<6;) e.push(null);
                                          if(e[2] == null) e[2] = this.onLoading;
                                          if(e[2] == null) e[3] = this.onError;
                                          if(e[2] == null) e[4] = this.onTimeout;
                                          if(e[2] == null) e[5] = this.onStateChanged;
                                          if(typeof e[0] == "function") {
                                                        return AjaxPro.queue.add(this.url, method, args, e);
                                          }
                            }
                            var r = new AjaxPro.Request();
                            r.url = this.url;
                            r.onLoading = this.onLoading;
                            r.onError = this.onError;
                            r.onTimeout = this.onTimeout;
                            r.onStateChanged = this.onStateChanged;
                            return r.invoke(method, args);
              }
};
AjaxPro.Request = Class.create();
AjaxPro.Request.prototype = {
              invoke: function(method, args, callback, context) {
                            this.__start = new Date().getTime();
                            this.isRunning = true;
                            this.method = method;
                            this.args = args;
                            this.callback = callback;
                            this.context = context;
  
                            if(MS.Debug.enabled == true)
                            MS.Debug.trace("Invoking " + method + "...");

                            var async = typeof callback == "function" && callback != AjaxPro.noOperation;
                            var json = AjaxPro.toJSON(args) + " ";

                            if(AjaxPro.cryptProvider != null)
                            json = AjaxPro.cryptProvider.encrypt(json);

                            if(async) {
                                                        this.xmlHttp.onreadystatechange = this.doStateChange.bind(this);
                                                        if(typeof this.onLoading == "function") this.onLoading(true);
                            }

                            this.xmlHttp.open("POST", this.url, async);
                            this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                            this.xmlHttp.setRequestHeader("Content-Length", json.length);
                            this.xmlHttp.setRequestHeader("Ajax-method", method);
  
                            if(AjaxPro.token != null && AjaxPro.token.length > 0)
                            this.xmlHttp.setRequestHeader("Ajax-token", AjaxPro.token);

                            if(MS.Browser.isIE)
                                                        this.xmlHttp.setRequestHeader("Accept-Encoding", "gzip, deflate");
                            else
                                                        this.xmlHttp.setRequestHeader("Connection", "close");  // Mozilla Bug #246651

                            if(this.onTimeout != null && typeof this.onTimeout == "function")
                            this.timeoutTimer = setTimeout(this.timeout.bind(this), AjaxPro.timeoutPeriod);

                            this.xmlHttp.send(json);
  
                            json = null;
                            args = null;
                            delete json;
                            delete args;
  
                            if(!async) {
                                                        return this.createResponse();
                            }
  
                            return true; 
              }
}
//当用户触发UI控件动作时
1.由于生成的客户端脚本中包含对于"/AjaxPro.Sample/ajaxpro/AjaxPro.Sample.Sample_1,AjaxPro.Sample.ashx"的调用,导致AjaxHandlerFactory.GetHandler被调用
2.在AjaxHandlerFactory.GetHandler中返回AjaxSyncHttpHandler
3..net运行框架在AjaxHandlerFactory.GetHandler返回不同的IHttpHandler实现(AjaxSyncHttpHandler等)时调用该实现的以下方法:
AjaxSyncHttpHandler.ProcessRequest方法被调用:
将调用转嫁到AjaxProcHelper.Run操作:
1.通过反射实现对指定C#类方法的调用.
2.将反射调用结果通过XmlHttpRequestProcessor.SerializeObject中对JavaScriptSerializer.Serialize的调用通过Response向客户端返回. 

 类似资料: