如何在Ajax回调中添加其他参数?

时间:2014-08-05 20:15:44

标签: jquery ajax

我想我可能已经做到了......不是以最好的方式。但它正在工作,结果将在我的 data 对象中返回。现在我需要传递一些额外的参数。如何使用其他参数调用handleData()?我试过这个:

handleData({'likes' : true})

但是删除了从Ajax调用返回的data对象。

这是我的代码:

// My global Ajax function to 'set' data
$.fn.setData = function(url, params){
    return $.ajax({
        url: site_url + url,
        type: "POST",
        dataType: 'json',
        data: params
    });
}    

function registerLike() {
    var like    = $('.interaction .like')
    var status  = $(like).data('status');
    var count   = $(like).data('count');

    like.click(function(){
        if(!liked){
            // I need to pass adition parameters here
            like.setData('action/like', {'id' : id}).done(handleData());
        } else {
            // and here
            like.setData('action/unlike', {'id' : id}).done(handleData());
        }
    });

    function handleData(data){
        if(data.success) {

            if(data.likes){
                count = count+1;
                liked = 1;
            } else {
                if(count > 0) {
                    count = count-1;
                } else {
                    count = 0;
                }
                liked = 0;
            }

            $('.interaction .like .icon').toggleClass('el-icon-heart');
            $('.like-count').html(count);
        }
    }
}

2 个答案:

答案 0 :(得分:4)

您始终可以使用匿名函数创建包装器:

like.setData('action/unlike', {'id' : id}).done(function(response){
    myCallback(response, extraData);
});

答案 1 :(得分:1)

Amir's suggestion是个好人。但是,它在Function.bind

中进行了概括
like.setData('action/unlike', {'id' : id}).done( 
    myCallback.bind(this, extraData) 
);

请注意,您的功能将被调用为myCallback(extraData, response),而不是myCallback(response, extraData)。如果你真的需要在调用时间参数之后传递extraData,你可以实现你自己的绑定,它附加绑定的参数而不是在它们之前。

Ext-JS有一个绑定函数,可以让你选择插入绑定参数的位置。

/**
 * Create a new function from the provided `fn`, change `this` to the provided scope, optionally
 * overrides arguments for the call. (Defaults to the arguments passed by the caller)
 *
 * {@link Ext#bind Ext.bind} is alias for {@link Ext.Function#bind Ext.Function.bind}
 *
 * @param {Function} fn The function to delegate.
 * @param {Object} scope (optional) The scope (`this` reference) in which the function is executed.
 * **If omitted, defaults to the default global environment object (usually the browser window).**
 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
 * if a number the args are inserted at the specified position
 * @return {Function} The new function
 */
bind: function(fn, scope, args, appendArgs) {
    if (arguments.length === 2) {
        return function() {
            return fn.apply(scope, arguments);
        };
    }

    var method = fn,
        slice = Array.prototype.slice;

    return function() {
        var callArgs = args || arguments;

        if (appendArgs === true) {
            callArgs = slice.call(arguments, 0);
            callArgs = callArgs.concat(args);
        }
        else if (typeof appendArgs == 'number') {
            callArgs = slice.call(arguments, 0); // copy arguments first
            Ext.Array.insert(callArgs, appendArgs, args);
        }

        return method.apply(scope || Ext.global, callArgs);
    };
},

请参阅http://docs-origin.sencha.com/extjs/4.2.2/#!/api/Ext.Function-method-bind