views:

74

answers:

1

I have a chain of asynchronous events that i want to execute, some of them fetch commands. I understand the basics of using dojo.Deferred, but all of my attempts so far to add a fetch callback have failed...

What im trying to do is 1) add a fetch call to my Deferred object 2) have that fetch function be run, possibly with some extra arguments and 3) pass on the 'fetched' values to the next callback in the chain... Here's what i've got:

var myDeferred = new dojo.Deferred();
myDeferred.addCallback(this.myStore.fetch({query:{//some query//}, 
    onComplete: function(items) { return items } })); //add the fetch callback? i think this is wrong...
myDeferred.addCallback(function(items) { console.log(items) }); //log the fetched items

myDeferred.callback(); //to fire
+1  A: 

you have to fire your deferred from the callback of the fetch

var myDeferred = new dojo.Deferred();
this.myStore.fetch({
    query:{//some query//}, 
    onComplete: function(items) { myDeferred.callback(items); }
})
myDeferred.addCallback(function(items) { console.log(items) }); //log the fetched items

If you have multiple fetches you should look into DeferredList and into returning a Deferred from the deferred-callback. Both can be used to create quite complex behavior. You should also look into the new dojo 1.5 Deferred which have a simpler usage pattern, which might help you.

squiddle