zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    jQuery实现鼠标点击Div区域外隐藏Div
    JS判断输入值为正整数
    trim()不兼容ie的问题及解决方法
    傻问题就用傻办法:解决问题有时候不需要探究根源,依据表象就能直接解决
    /vendor/lib64/libOpenCL.so在安卓应用中无访问权限的解决办法
    复数域上的人工神经网络与量子计算
    中国移动CMCC家庭路由器的默认登陆账号
    717. 1-bit and 2-bit Characters
    219. Contains Duplicate II
    1346. Check If N and Its Double Exist
  • 原文地址:https://www.cnblogs.com/aarond/p/ajax.html
Copyright © 2011-2022 走看看