Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass extra parameters to jquery.Deferred callback

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.

like image 376
Asciiom Avatar asked Sep 07 '12 09:09

Asciiom


1 Answers

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.

like image 86
freakish Avatar answered Sep 28 '22 07:09

freakish