将额外的参数传递给jquery.Deferred回调

Asc*_*iom 6 javascript jquery callback jquery-deferred

我想将一些额外的参数传递给jQuery.Deferred完成的回调,我现在这样做:

//dfd gets defined here as the return value of jQuery.ajax

var me = this;
var selector = $("#selector");

dfd.done(function(response){
    me.updated(response, selector);
});
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更好的方法来做到这一点?我以为我已经阅读过一些关于传递参数的更简洁方法,而不需要匿名包装函数,但我不能为我的生活记住我在哪里阅读,或者我读到的内容.到目前为止谷歌搜索没有任何结果.

fre*_*ish 10

为了通过东西.done回调需要传递它.resolve,例如

dfd.done( function(selector) {
   console.log( selector );
});
dfd.resolve( selector );
Run Code Online (Sandbox Code Playgroud)

但在你的情况下dfd是一个$.ajax对象,并.resolve在内部调用,所以你无法控制它.因此,唯一的方法是使用匿名函数和闭包.

顺便说一句:这个解决方案没有什么不洁净的.