国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

My ajaxwrapper tool

2019-11-15 02:28:07
字體:
供稿:網(wǎng)友

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.


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武宁县| 喀什市| 河东区| 东源县| 托克逊县| 积石山| 商都县| 沂南县| 乌拉特后旗| 海门市| 景谷| 大足县| 赤水市| 兴义市| 磐石市| 平山县| 铜梁县| 德令哈市| 区。| 肃北| 子长县| 邹平县| 武安市| 黑龙江省| 云梦县| 绥德县| 永宁县| 资阳市| 永善县| 西乡县| 广水市| 丽水市| 东乌| 河西区| 古田县| 离岛区| 芜湖县| 伊川县| 郎溪县| 台山市| 巨野县|