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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With