Ricardo,
Your loosing me on what you actually want to do - and truthfull, I think SPService already does it. Your last comment - "realize that nested ajax calls in the traditional completefunc would also get left off the deferred object" - tells me you are doing something more complex than the simple example you listed above.
So here is an example that will show you this - both the order in with the callbacks are executed and how you could control the XML Document response for down-stream callbacks:
So... :-)
With all that said and tested - can you post a code sample of something that is not working for you?
I'm really having a hard time understanding what is not working.
/Paul
Ricardo,
Your loosing me on what you actually want to do - and truthfull, I think SPService already does it. Your last comment - "realize that nested ajax calls in the traditional completefunc would also get left off the deferred object" - tells me you are doing something more complex than the simple example you listed above.
Re: Use of completefunc and Promise callbacks
Although you should not, you can actually use both a completefunc callback and define a promise callback - but you should realize how those are executed and obviously how to use them effectively. jQuery .ajax() method will do the following when it is done, and in this order:- Resolve the ajax Deferred - which turn executes any of the promise callbacks
-
If a 'complete' callback is defined - execute it
So here is an example that will show you this - both the order in with the callbacks are executed and how you could control the XML Document response for down-stream callbacks:
$().SPServices({
operation: "GetList",
listName: "Tasks",
completefunc: function (xData, Status) {
console.log("completefunc()");
console.log(xData.responseXML.paul); // notice I'm printing a property called "paul" of the XML Document returned by .ajajx()/SPServices
}
})
.done(function(){
console.log("done()");
})
.then(function (xml, Status) {
console.log("then()", xml, Status);
xml.paul = "tavares"; // pass data to others in the chain via the .ajax() input params objects
})
.then(function(){
console.log("then(2)");
})
.done(function(){
console.log("done(1)");
})
.then(function(){
console.log("then(3)");
});
The output of this call in the console would be:done()
then() Document success
then(2)
done(1)
then(3)
completefunc()
tavares
Use of multiple SPServices calls with jQuery .when()
The promise returned by SPServices (which is the one returned by .ajax) can in fact be chained and queued up with $.when(). Here is an example:$.when(
// Call 1
$().SPServices({
operation: "GetList",
listName: "Tasks",
completefunc: function (xData, Status) {
console.log("completefunc()");
}
})
.done(function(){
console.log("done()");
}),
// Call 2
$().SPServices({
operation: "GetList",
listName: "Tasks",
completefunc: function (xData, Status) {
console.log("completefunc(2)");
}
})
.done(function(){
console.log("done(2)");
})
)
.then(function(){
console.log("ALL PROMISES DONE!!!!!");
});
Then output of this would be:done()
completefunc()
done(2)
ALL PROMISES DONE!!!!!
completefunc(2)
This again shows that the deferred callbacks that are queued up will executed serially and in the order that they are added to the queue... In looking at the output you may question why 'complete(2)' executed after 'ALL PROMISES DONE!!! and not after "done(2)". That again is because once the second "done(2)" is executed, the .when() Deferred is resolved, and thus its callbacks are executed prior to givein control back to the last .ajax()... So... :-)
With all that said and tested - can you post a code sample of something that is not working for you?
I'm really having a hard time understanding what is not working.
/Paul