Until recently, when I write ajax call, always write like below:

$.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.

最新文章

  1. 利用JSON实现数组去重
  2. 7.10 数据注解特性--NotMapped
  3. Python for Informatics 第11章 正则表达式四(译)
  4. How to use aws CloudFront for CDN
  5. hbase基础-系统架构
  6. block使用小结、在arc中使用block、如何防止循环引用
  7. android 学习(1)
  8. JMeter学习(二)录制脚本
  9. 冲突--ScrollView嵌套ListView冲突问题的最优解决方案
  10. java--依赖、关联、聚合和组合之间区别的理解
  11. bzoj2242
  12. Contest20140705 testA 二分
  13. 1、CentOS6.5系统安装及学习
  14. jquery插件FlexiGrid的使用
  15. linux命令学习7-jstat命令
  16. 被低估的.net(上) - 微软MonkeyFest 2018广州分享会活动回顾
  17. tensorflow实现mnist
  18. Ubuntu18.10下运行blender2.80bate闪退(问题?)
  19. 【java】函数概述
  20. H5 设备方向及运动API

热门文章

  1. Android 9.0新特性
  2. html + css3 demo
  3. mac 全角/半角标点符号切换
  4. 服务器 'xxxx' 已被定义为分发服务器。若要将该服务器重新配置为分发服务器,必须首先卸载现有的分发服务
  5. Response()的对象
  6. KMP算法详解-彻底清楚了(转载+部分原创)
  7. win7 中 sql server2005 卸载简介
  8. Windows 版本说明,Enterprise、Ultimate、Home、Professional知多少
  9. Angular之模态弹窗ui-bootstrap-modal及轻量级弹窗ngDialog
  10. WPF自定义控件(三)の扩展控件