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
.donecallback you need to pass it in.resolve, for examplebut in your case
dfdis a$.ajaxobject and.resolveis 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.