Ajax之工厂模式封装XMLHttpRequest
来源:互联网 发布:arm linux 可运行程序 编辑:程序博客网 时间:2024/05/12 17:36
如下是对XMLHttpRequest的封装,为一个简易的工厂模式/* AjaxHandler interface. */var AjaxHandler = new Interface('AjaxHandler', ['request', 'createXhrObject']);/* SimpleHandler class. */var SimpleHandler = function() {}; // implements AjaxHandlerSimpleHandler.prototype = { request: function(method, url, callback, postVars) { var xhr = this.createXhrObject(); xhr.onreadystatechange = function() { if(xhr.readyState !== 4) return; (xhr.status === 200) ? callback.success(xhr.responseText, xhr.responseXML) : callback.failure(xhr.status); }; xhr.open(method, url, true); if(method !== 'POST') postVars = null; xhr.send(postVars); }, createXhrObject: function() { // Factory method. var methods = [ function() { return new XMLHttpRequest(); }, function() { return new ActiveXObject('Msxml2.XMLHTTP'); }, function() { return new ActiveXObject('Microsoft.XMLHTTP'); } ]; for(var i = 0, len = methods.length; i < len; i++) { try { methods[i](); } catch(e) { continue; } // If we reach this point, method[i] worked. this.createXhrObject = methods[i]; // Memoize the method. return methods[i]; } // If we reach this point, none of the methods worked. throw new Error('SimpleHandler: Could not create an XHR object.'); } };/* Usage. */var myHandler = new SimpleHandler();var callback = { success: function(responseText) { alert('Success: ' + responseText); }, failure: function(statusCode) { alert('Failure: ' + statusCode); } };myHandler.request('GET', 'script.php', callback);
如下是辅助类的实现:
// Constructor.var Interface = function(name, methods) { if(arguments.length != 2) { throw new Error("Interface constructor called with " + arguments.length + "arguments, but expected exactly 2."); } this.name = name; this.methods = []; for(var i = 0, len = methods.length; i < len; i++) { if(typeof methods[i] !== 'string') { throw new Error("Interface constructor expects method names to be " + "passed in as a string."); } this.methods.push(methods[i]); } }; // Static class method.Interface.ensureImplements = function(object) { if(arguments.length < 2) { throw new Error("Function Interface.ensureImplements called with " + arguments.length + "arguments, but expected at least 2."); } for(var i = 1, len = arguments.length; i < len; i++) { var interface = arguments[i]; if(interface.constructor !== Interface) { throw new Error("Function Interface.ensureImplements expects arguments " + "two and above to be instances of Interface."); } for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) { var method = interface.methods[j]; if(!object[method] || typeof object[method] !== 'function') { throw new Error("Function Interface.ensureImplements: object " + "does not implement the " + interface.name + " interface. Method " + method + " was not found."); } } } };
0 0
- Ajax之工厂模式封装XMLHttpRequest
- JS-封装 ajax XmlHttpRequest
- Ajax技术之Ajax重构(封装XMLHttpRequest)
- Ajax定义一个XMLHttpRequest工厂
- Ajax应用示例: XMLHttpRequest封装
- ajax之xmlHttpRequest
- AJAX之xmlHttpRequest
- ajax之XMLHttpRequest
- AJAX之XMLHttpRequest对象
- Ajax之XmlHttpRequest对象
- Ajax核心技术之XMLHttpRequest
- AJAX之XMLHttpRequest
- Ajax之XMLHttpRequest对象
- AJAX之XMLHttpRequest对象
- AJAX之XMLHttprequest对象
- AJAX之XMLHttpRequest对象
- Ajax之XMLHttpRequest
- AJAX设计模式 XMLHttpRequest调用
- 南方的冬天
- 英语学习方法
- 我的电脑图标问题
- spring的rmi整合以及客户端和服务端搭建2.0
- 初识STORM 快速入手
- Ajax之工厂模式封装XMLHttpRequest
- 关于.Net MVC 中_Layout.cshtml页面新布局的几点看法
- code first 迁徙数据
- Qt国际化
- grails学习笔记-7、Domain创建过程中注意的问题
- mysql建表常用sql语句分享
- NavigationController单个界面横屏,其他全竖屏
- java 八字计算
- santoku的安装问题