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 ...
随机推荐
- Android内存优化(二)解析Memory Monitor、Allocation Tracker和Heap Dump
前言 要想做好内存优化工作,就要掌握两大部分的知识,一部分是知道并理解内存优化相关的原理,另一部分就是善于运用内存分析的工具.本篇就来介绍内存分析工具:Memory Monitor.Allocatio ...
- 你不可不知的Java引用类型之——SoftReference源码详解
定义 SoftReference是软引用,其引用的对象在内存不足的时候会被回收.只有软引用指向的对象称为软可达(softly-reachable)对象. 说明 垃圾回收器会在内存不足,经过一次垃圾回收 ...
- C#调用原生C++ COM对象(在C++中实现C#的接口)
为了跨平台在.net core中使用COM,不能使用Windows下的COM注册机制,但是可以直接把IUnknown指针传给C#,转换为指针,再转换为C#的接口(interface). 做了这方面的研 ...
- Windows服务器防火墙配置规范
本文属于一篇内部规范文档,整理的初衷是为了规范.统一集团的Windows服务器(仅仅SQL Server数据库服务器)防火墙设置,仅仅供内部其它同事设置Windows防火墙时作为参考的文档资料.如有不 ...
- HTML—标签与表格 、框架
1.标签 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- sql语句中的join用法(可视化解释)
一.innerjoin innerjoin总结来说就是 ,如A知道通往B如何走:B知道通往C如何走:但是A不知道通往C如何走,但是A可以通过B获得去往C的通往方式.. 首先,假设有A,B两张表,结构及 ...
- Essential pro angular and asp.net core 笔记
1. dotnet ef相关命令 删除数据库(适合只有一个数据库的情形) dotnet ef database drop --force 更新数据库(适合只有一个数据库的情形) dotnet ef d ...
- PostgreSQL远程访问设置
数据库版本:9.3.23(Windows xp系统) 步骤: 1.需要修改数据库安装目录下的pg_hba.conf文件 修改成: 2.并使用psql执行pg_ctl reload重新加载配置文件
- Call to localhost/127.0.0.1:9000 failed on connection exception:java.net.ConnectException的解决方案
Call to localhost/127.0.0.1:9000 failed on connection exception:java.net.ConnectException的解决方案 作者:凯鲁 ...
- python 浅谈字典dict
一.字典简介 字典(dict)是python中唯一的映射类型,他是以{ }括起来的键值对组成,在dict中的key是唯一的.在保存的时候,根据key来计算出一个内存地址.然后将key-value保存在 ...