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. Python __init__.py文件的作用

    我们经常在python的模块目录中会看到 "__init__.py"  这个文件,那么它到底有什么作用呢? 1. 模块包(module package)标识 如果你是使用pytho ...

  2. Orchard详解--第五篇 CacheManager

    上一篇文章介绍了Orchard中的缓存,本篇主要针对CacheManager进行分析,CacheManager在Orchard中用于存储应用程序的配置信息以及框架内部的一些功能支持,包括整个拓展及拓展 ...

  3. Spark之UDF

    package big.data.analyse.udfudaf import org.apache.spark.sql.types.{IntegerType, StringType, StructF ...

  4. [20170612]FOR ALL COLUMNS SIZE repeat(12c).txt

    [20170612]FOR ALL COLUMNS SIZE repeat(12c).txt --//昨天看了https://jonathanlewis.wordpress.com/2017/06/0 ...

  5. shell编程-输出(六)

    echo输出echo指令用于字符串的输出 格式:echo 字符串 直接输出字符串:string echo 'this is string-output' 用双引号,这儿也可以省略引号 转义字符:\ e ...

  6. ArcGIS Server10.2 集群部署注意事项

    不接触Server很久了,最近一个省级项目需要提交一个部署方案,由于是省级系统,数据.服务数量都较大,需要考虑采用Server集群的方式来实现.在网上搜罗了以下Server集群的资料,按照步骤一步步来 ...

  7. c/c++ 标准库 string

    c/c++ 标准库 string 标准库 string的小例子 test1~test10 #include <iostream> using namespace std; int main ...

  8. openSUSE Leap 15.0 Adobe Flash Player 安装说明

    鉴于Firefox安装配置文件: mozilla_lib=file $MOZ_PROGRAM LIB=lib -bit.*(x86-|S/|PowerPC|ARM aarch64)’ &&am ...

  9. C语言经典例题(菜鸟教程100例)

    学习c语言基础,怎么能少了菜鸟教程上的100道例题呢,这里整理一下每道题的链接,希望大家能享受学习的乐趣 1,有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 2,企业发放 ...

  10. May 29. 2018 Week 22nd Tuesday

    Nothing is more terrible than ignorance in action. 最可怕的事情莫过于无知而行动. In today's digital age, we can ru ...