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.

My ajaxwrapper tool的更多相关文章

  1. [免费了] SailingEase .NET Resources Tool (.NET 多语言资源编辑器)

    这是我2010年左右,写 Winform IDE (http://www.cnblogs.com/sheng_chao/p/4387249.html)项目时延伸出的一个小项目. 最初是以共享软件的形式 ...

  2. jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the install tool.

    jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the ...

  3. mtk flash tool,Win7 On VirtualBox

    SP_Flash_Tool_exe_Windows_v5.1624.00.000 Win7 在 VirtualBox, 安裝 mtk flash tool, v5.1628 在燒錄時會 fail. v ...

  4. 使用Microsoft Web Application Stress Tool对web进行压力测试

    Web压力测试是目前比较流行的话题,利用Web压力测试可以有效地测试一些Web服务器的运行状态和响应时间等等,对于Web服务器的承受力测试是个非常好的手法.Web 压力测试通常是利用一些工具,例如微软 ...

  5. How to Use Android ADB Command Line Tool

    Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Andro ...

  6. 使用MAT(Memory Analyzer Tool)工具分析dump文件--转

    原文地址:http://gao-xianglong.iteye.com/blog/2173140?utm_source=tuicool&utm_medium=referral 前言 生产环境中 ...

  7. Linux Cmd Tool 系列之—script & scriptreplay

    Intro Sometime we want to record cmd and outputs in the interactive shell sessions. However history ...

  8. [Tool] SourceTree操作中遇到错误(Filename too long)的解决方案

    [Tool] SourceTree操作中遇到错误(Filename too long)的解决方案 问题情景 使用SourceTree,可以方便开发人员使用图形化接口的Git指令来管理原始码.但是在Wi ...

  9. You must use the Role Management Tool to install or configure Microsoft .NET Framework 3.5 SP1

    今天在Windows Server 2008 下安装SQL SERVER 2008时,碰到如下错误: You must use the Role Management Tool to install ...

随机推荐

  1. Visual Studio未能加载“XX”包的解决方案

    1.打开命令行(管理员) 2.进入到devenv.exe所在的目录u 3.输入devenv,会出现帮助提示 4.将所有带“Reset..”的命令执行一遍 5.OK 我尝试过使用修复功能,貌似没有作用. ...

  2. Centos 中无法上网的问题

    我是 Centos 最小化安装的,安装网后 Centos 竟然无法上网...有点奇葩, 应该是网卡没有激活的问题了,下面是解决的过程 查看网卡 ip addr 其中 lo 是 Loop back ad ...

  3. HtmlAgilityPack 的东西

    之前都是用正则抓取页面,本人正则不咋地,有些东西用抓取来很费劲,呵呵 在网上看到别人推荐一个 HtmlAgilityPack 的东西,网上找了资料,自己写了个抓取网页的例子,框架用的ASP.NET M ...

  4. HBase架构设计

    一.Client 包含访问HBase的接口并维护cache来加快对HBase的访问. 二.Zookeeper 1.保证任何时候,集群中只有一个master. 2.存储所有Region的寻址入口. 3. ...

  5. javaweb分页查询实现

    Javaweb分页技术实现 分页技术就是通过SQL语句(如下)来获取数据,具体实现看下面代码 //分页查询语句 select * from 表名 where limit page , count; 和 ...

  6. java调用天气预报接口案例

    免费天气接口:http://mobile.weather.com.cn/data/sk/城市ID.html 例如: http://mobile.weather.com.cn/data/sk/10124 ...

  7. Greenplum hostname和address不一致导致配置文件无法加载

    最近又遇到了几个坑,逐一记录分析下. 1.主机名hostname和address不一致 在又一次部署压测环境交由测试组进行压测时,同事修改了pg_hba.conf文件重新加载配置文件时报错.(找不到l ...

  8. Python操作字典(dict)

    一.字典定义 >>> dict={} 二.字典元素添加 >>> dict['性别']='男' >>> dict {'性别': '男'} >& ...

  9. Elasticsearch拼音和ik分词器的结合应用

    一.创建索引时,自定义拼音分词和ik分词 PUT /my_index { "index": { "analysis": { "analyzer&quo ...

  10. numpy中stack、hstack,vstack,dstack函数功能解释

    https://blog.csdn.net/Riverhope/article/details/78922006 https://blog.csdn.net/ygys1234/article/deta ...