I'm still trying to wrap my head around deferred
and what not, so with this in mind I have a question on how to do the following.
My team and I have 3 separate .load()
methods that each go grab a specific template and append that to the same container. Each load takes a different amount of time as you might think, so when the content loads, it loads in a 'stair step' fashion (1, then 2, then 3). I'd like to make use of deferred
objects and wait until they are all done, then append them at the same time to remove the 'stair step' action.
$('<div>').load(baseInfoTemplate, function () {
var baseData = {
// build some object
};
$.tmpl(this, baseData).appendTo($generalContainer);
});
All three calls are similar to the call above.
How can I achieve this?
I use the following code as a generic library
then call it as the first function in the application's main js file.
I use next code for this case:
To my mind it looks more structured and pretty nice than previous implementations.
$.load()
isn't designed for use with Deferred objects, and also it is intended specifically to drop stuff into the page immediately.To solve that latter problem you either have to render the entire container outside the DOM, and then drop it in when they're all done, or you need to accumulate the three results and then put them all in in one go.
The process below uses the latter approach:
Use
$.get()
instead, and create an array of thejqXHR
objects returned by$.get()
Store the return fragments from each
$.get()
in an array tooUse
$.when.apply($, myArray).done(function() { ... })
to apply the templates and put them into the DOMSee http://jsfiddle.net/alnitak/WW3ja/
Here's a Gist for a little jQuery plugin that adds a loadThen function to a set of jQuery elements. It's basically load() without the callback and it returns a promise that is only resolved after the content is loaded and inserted into the set of selected elements.
It's basically a copy/paste of jQuery's own load() code except it returns the promise from the actual ajax call. This lets you get a rejected promise if the ajax fails.
Since it's based on the load() functionality, you can add a selector after the url seperated by a space to get only a fragment of the loaded html.
Example 1: Load the home page of this site into element with id="container"
Example 2: Load the home page's header into this page's header
Gist contents:
You can store the
promise objects
in an array and use$.when()
to find out if those promises are fullfilled. This could look like this:I'm using
.apply()
here because it accepts an array as arguments for a function call. So basically, we're passing all promises objects to.when()
.Example: http://jsfiddle.net/Hg77A/1/
Update:
as Alnitak pointed out, the above example wouldn't make much sense if there is no "success" callback handler. If it is just enough to fire the "all done" handler after you transfered the data with
.load()
, you just would need to.resolve()
the promises within thesuccess handler
from.load()
. Does that make any sense?