JQuery - $.when syntax for array of Deferred objects -


this question has answer here:

it first time using $.when , having difficulty syntax.
have code similar simplified example below. works (if haven't caused error when simplified it). problem don't know home many elements customerids array contain.

var customerids = new [1, 2, 3];  $.when(     getcustomerdata(customerids[0]),     getcustomerdata(customerids[1]),     getcustomerdata(customerids[2]) ).then(function() {     alert('success'); }).fail(function() {     alert('error'); });  function getcustomerdata(int id) {     return new $.deferred(function(defer) {                     dosomework(id, defer);     }).promise();        } 

i write $.when statement follows having difficulty getting syntax right.

$.when(     getcustomerdatacalls(customerids), ).then(function() {     alert('success'); }).fail(function() {     alert('error'); }); 

where getcustomerdatacalls implemented as:

function getcustomerdatacalls(customerids) {     var dfds = [];      (var id in customerids) {         dfds.push(new $.deferred(function(defer) {                                     dosomework(id, defer);                                  }).promise());          }      return dfds; } 

unfortunately wrong implementation , cannot work out going wrong. best guess going wrong when returning array of deferreds

update:
updated code after lanzz mentioned contrived example returns deferred, updated example include dosomework

yes, have stumbled upon well: when not allow passed array. use apply achieve desired result.

$.when.apply($, getcustomerdatacalls(customerids)) 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -