My ajaxwrapper tool
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的更多相关文章
- [免费了] SailingEase .NET Resources Tool (.NET 多语言资源编辑器)
这是我2010年左右,写 Winform IDE (http://www.cnblogs.com/sheng_chao/p/4387249.html)项目时延伸出的一个小项目. 最初是以共享软件的形式 ...
- 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 ...
- mtk flash tool,Win7 On VirtualBox
SP_Flash_Tool_exe_Windows_v5.1624.00.000 Win7 在 VirtualBox, 安裝 mtk flash tool, v5.1628 在燒錄時會 fail. v ...
- 使用Microsoft Web Application Stress Tool对web进行压力测试
Web压力测试是目前比较流行的话题,利用Web压力测试可以有效地测试一些Web服务器的运行状态和响应时间等等,对于Web服务器的承受力测试是个非常好的手法.Web 压力测试通常是利用一些工具,例如微软 ...
- 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 ...
- 使用MAT(Memory Analyzer Tool)工具分析dump文件--转
原文地址:http://gao-xianglong.iteye.com/blog/2173140?utm_source=tuicool&utm_medium=referral 前言 生产环境中 ...
- Linux Cmd Tool 系列之—script & scriptreplay
Intro Sometime we want to record cmd and outputs in the interactive shell sessions. However history ...
- [Tool] SourceTree操作中遇到错误(Filename too long)的解决方案
[Tool] SourceTree操作中遇到错误(Filename too long)的解决方案 问题情景 使用SourceTree,可以方便开发人员使用图形化接口的Git指令来管理原始码.但是在Wi ...
- 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 ...
随机推荐
- 配置 Sublime Text 3 作为Python R LaTeX Markdown IDE
配置 Sublime Text 3 作为Python R LaTeX Markdown IDE 配置 Sublime Text 3 作为Python IDE IDE的基本功能:代码提醒.补全:编译文件 ...
- coTurn 运行在Windows平台的方法及服务与客户端运行交互流程和原理
coTurn是一个开源的STUN和TURN及ICE服务项目,只是不支持Windows.为了在window平台上使用coTurn源码,需要在windows平台下安装Cygwin环境,并编译coTurn源 ...
- mssql sqlserver 从指定字符串中获取数字的方法
转自:http://www.maomao365.com/?p=6410 摘要: 下文主要分享从指定字符串或列中获取数字信息,如下所示: 实验环境:sql server 2000 ----编写sql函数 ...
- mssql sqlserver 批量删除所有存储过程的方法分享
转自:http://www.maomao365.com/?p=6864 摘要: 下文讲述采用sql脚本批量删除所有存储过程的方法,如下所示: 实验环境:sqlserver 2008 R2 平常使用sq ...
- c/c++ 标准库 map set 大锅炖
标准库 map set 大锅炖 一,关联容器有哪些 按关键字有序保存元素 map 保存key和value set 只保存key mulutimap key可以重复出现 multiset key可以重复 ...
- Selenium Webdriver 中的 executeScript 使用方法
1.使用executeScript 返回一个WebElement . 下例中我们将一个浏览器中的JavaScript 对象返回到客户端(C#,JAVA,Python等). IWebElement el ...
- 从零开始的cve分析- cve-2016-0095 简易记录
0x00 前言 看k0shl大佬的SSCTF pwn450 Windows Kernel Exploitation Writeup一文,试着写一个x64下的poc. poc地址:https://git ...
- KFCM算法的matlab程序(用FCM初始化聚类中心)
KFCM算法的matlab程序(用FCM初始化聚类中心) 在“聚类——KFCM”这篇文章中已经介绍了KFCM算法,现在用matlab程序对iris数据库进行实现,用FCM初始化聚类中心,并求其准确度与 ...
- 附2 rabbitmq用户管理、角色管理与权限管理
本文摘自:http://my.oschina.net/hncscwc/blog/262246 1. 用户管理 用户管理包括增加用户,删除用户,查看用户列表,修改用户密码. 相应的命令 (1) 新增一个 ...
- 如何设计一个"好的"测试用例?
什么才算是“好的”测试用例? 好的测试用例一定是一个完备的集合,它能够覆盖所有等价类以及各种边界值,而跟能否发现缺陷无关. "好的"测试用例必须具备哪些特征? 一个“好的”测试用例 ...