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. Android内存优化(二)解析Memory Monitor、Allocation Tracker和Heap Dump

    前言 要想做好内存优化工作,就要掌握两大部分的知识,一部分是知道并理解内存优化相关的原理,另一部分就是善于运用内存分析的工具.本篇就来介绍内存分析工具:Memory Monitor.Allocatio ...

  2. 网站的增 / 删 / 改 / 查 时常用的 sql 语句

    最近在学习数据库 php + mysql 的基本的 crud 的操作,记录碰到的坑供自己参考.crud中需要用到的sql语句还是比较多的,共包括以下几个内容: 查询所有数据 查询表中某个字段 查询并根 ...

  3. 个人项目:Java实现WC

    Java实现WC Github项目地址:https://github.com/auxshaw/WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个 ...

  4. java----堆区、方法区和栈区

    堆区:只存放类对象,线程共享: 方法区:又叫静态存储区,存放class文件和静态数据,线程共享; 栈区:存放方法局部变量,基本类型变量区.执行环境上下文.操作指令区,线程不共享; class A { ...

  5. [20190329]探究sql语句相关mutexes补充2.txt

    [20190329]探究sql语句相关mutexes补充2.txt --//昨天测试sql语句相关mutexes,看看如果出现多个子光标的情况. 1.环境:SCOTT@book> @ ver1P ...

  6. 验证对Random的两个猜想

    猜想1:Random.Next()产生的随机数不会有重复. 猜想2:大量级执行Random.Next(int i)分布在各个数值上的概率是均匀的. 验证猜想1 /*如果Random.Next()产生的 ...

  7. SSIS使用事务回滚

    --创建表ttt Create table ttt ( ID INT PRIMARY KEY , NAME VARCHAR(50) ) --插入测试数据 INSERT INTO TTT VALUES ...

  8. c/c++ 重载运算符的思考

    c/c++ 重载运算符的思考 #include <iostream> using namespace std; class Imaginary{ public: Imaginary():r ...

  9. SAP 维护视图创建与修改

    维护视图创建与修改 维护视图创建 T-CODE:SE54 维护ABAP数据字典 维护已生产的对象 注意:当维护视图修改后,需要删除已生成的对象,重新创建已生成的对象,否则无法显示,这个小窍门我花了半天 ...

  10. March 10th, 2018 Week 10th Saturday

    All good things must come to an end. 好景无常. Love is when the other person's happiness is more importa ...