My ajaxwrapper tool
2014-01-17 13:43
435 查看
Until recently, when I write ajax call, always write like below:
and repeat everywhere... Until some day: so much redundant code!
Fournately, the "ajaxwrapper" tool can resolve this problem. ^.^
By using "ajaxwrapper", the code will be changed like this:
I believe you'v found something missed--> we should define "ajaxDefinationId" first, like below:
Explain- core code:
Code is simple. First, it search ajax's global defination & current definatio, and then invoke jquery's ajax method.
Let's look error handler: a2d.core.exception.service.takeoverFunction, this function can add a wrapper on a function. When an error throw in function, takeoverFunction will catch it, and process it. See below:
Code is still simple. Core code is "try/catch"-->a2d.core.events.service.publish("a2d.core.exception:occurred", ex);
AhHa, finally, we found the error was published by a2d framework. Depend on this mechanism, the concrete impl be decopouled by pub/sub pattern, we can subscribe this event flexible.
The tool has been integrated into A2DFramework.
$.ajax({ type: "post", datatype: "json", url: "someurl", success: function (data) { //some logic } });
and repeat everywhere... Until some day: so much redundant code!
Fournately, the "ajaxwrapper" tool can resolve this problem. ^.^
By using "ajaxwrapper", the code will be changed like this:
a2d.core.ajax.ajaxwrapper("ajaxDefinationId", { userId: 100 }, function(result){ //some logic }).call();
I believe you'v found something missed--> we should define "ajaxDefinationId" first, like below:
a2d.core.ajax.ajaxwrapper.setup.add({ id: "ajaxDefinationId", method: "post", url: "testurl.aspx" });//we may extend here, add much more parameters like headers, etags, cache, etc...
Explain- core code:
a2d.core.ajax.ajaxwrapper = function (id, data, callback) { var defaultConfig = { id: null, data: null, callback: null }; var realConfig = $.extend(defaultConfig, { id: id, data: data, callback: callback }); var setupConfig = a2d.core.ajax.ajaxwrapper.setup.find(realConfig.id); var ajaxCall = function () { $.ajax({ url: setupConfig.url, type: setupConfig.method, async: true, cache: false, data: realConfig.data, dataType: "json", success: realConfig.callback, error: a2d.core.exception.service.takeoverFunction(function () { throw new kxtx.core.exception("ajax error"); }) }); } return { call: ajaxCall }; };
Code is simple. First, it search ajax's global defination & current definatio, and then invoke jquery's ajax method.
Let's look error handler: a2d.core.exception.service.takeoverFunction, this function can add a wrapper on a function. When an error throw in function, takeoverFunction will catch it, and process it. See below:
a2d.core.exception.service.takeoverFunction = function (fn) { var newHandler = function () { try { fn.call(fn, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9], arguments[10]); } catch (ex) { if (ex instanceof a2d.core.exception) { a2d.core.events.service.publish("a2d.core.exception:occurred", ex); } else { alert("未知exception类型"); } } }; return newHandler; }
Code is still simple. Core code is "try/catch"-->a2d.core.events.service.publish("a2d.core.exception:occurred", ex);
AhHa, finally, we found the error was published by a2d framework. Depend on this mechanism, the concrete impl be decopouled by pub/sub pattern, we can subscribe this event flexible.
The tool has been integrated into A2DFramework.
相关文章推荐
- Welcome to my comparison of AJAX frameworks for ASP.NET
- My First Glance At AJAX
- AJAX .Net Wrapper usage guide
- ascx + wrapper page + jQuery的Ajax解决方案
- 【转】ascx + wrapper page + jQuery的Ajax解决方案
- ajax,training my javascript!手写AJAX,用POST方法传值
- AJAX Wrapper for .NET
- my Ajax.js
- My tool chain in CentOS 7
- ascx + wrapper page + jQuery的Ajax解决方案
- My C++ Wrapper for FreeTDS
- [Drupal] My method of using ajax in Drupal
- [AJAX]40+ Tooltips Scripts With AJAX, JavaScript & CSS
- asp.net webForm也可以这样用Ajax -- My Ajax Framework [全屏看文]
- [Linux.Shell] My Shell Tool - Support for UI CheckBox
- myTestPractice 母版子页Ajax
- My First Django Project - <Django + MySQL + Ajax> (1)
- http://my-yii.blogspot.com/2013/09/yii-how-to-open-modal-via-ajax-via.html
- My Andoid Tool 微信禁用方案记录
- JAVA类,生成Json数组,ajax传入多组json数据。【MyJsonArray】