I have the following method added to an object :
loadAPIServiceData : function(service, format, term, callback) {
return $.when($.ajax({
url: service + ((term) ? term : '') + '?format=' + format
})).done(function(result) {
return callback.apply(result, [ result ]);
});
},
The callback function is passed one parameter that I need further down in the chain when I’m loading the data ( the result obviously gets whatever I get back from the AJAX call ). What I need is to be able to add more parameters to the callback function when it’s called, and still have the result in there 🙂
To be more explicit about it this is what I was meaning when I said “I need is to be able to add more parameters to the callback function when it’s called, and still have the result in there” :
var test = loadAPIServiceData('some_service', 'json', 'some_term', function(result, another_parameter, ...) {
});
You can use bind for apply arguments without need to modify the
loadAPIServiceData:If you prefer keep
resultin first arg position, this code send all additional argument to the callback, using the arguments object: