admin管理员组

文章数量:1287651

I want to pass some extra parameters to a jQuery.Deferred done callback, I'm doing it like this now:

//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);
});

I was wondering if there is a better way to do this? I thought I had read somewhere about a cleaner way to pass parameters without the need of an anonymous wrapper function but I can't for the life of me remember where I read it, or what I read. Google searches turned up nothing so far.

I want to pass some extra parameters to a jQuery.Deferred done callback, I'm doing it like this now:

//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);
});

I was wondering if there is a better way to do this? I thought I had read somewhere about a cleaner way to pass parameters without the need of an anonymous wrapper function but I can't for the life of me remember where I read it, or what I read. Google searches turned up nothing so far.

Share Improve this question asked Sep 7, 2012 at 9:00 AsciiomAsciiom 9,9757 gold badges41 silver badges59 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

In order to pass something to .done callback you need to pass it in .resolve, for example

dfd.done( function(selector) {
   console.log( selector );
});
dfd.resolve( selector );

but in your case dfd is a $.ajax object and .resolve is called internally, so you have no control over it. Therefore the only way to do that is to use anonymous function and closure.

By the way: there is nothing unclean about this solution.

本文标签: javascriptPass extra parameters to jqueryDeferred callbackStack Overflow