I have a product listing page and want to load up a detail view for each product via AJAX and display it within the page. I have a more complex set of conditions I want to set up animations with, but I'll keep it simple here.
Basically, upon click, I want to run the AJAX request and simultaneously animate (in some cases, a series of animations...) the content wrapper open and show a 'loading' state. Once BOTH of those are done, I then want to drop in, and animate the content. I thought jQuery Deferred Objects would be the way to go, but I don't have a good grasp on them and my attempts haven't gotten me where I want to be.
I tried something like this:
var dfr = $.Deferred();
dfr.then(function() { return wrapper.fadeIn(5000); });
$.when(dfr, $.get('/detail.html'))
.then(function() {
console.log('All done, run additional animations...');
});
dfr.resolve();
... but it gets triggered as soon as the AJAX request is done, even if the animation is still running.
I also tried this:
var dfr = $.Deferred();
dfr.then(function() { return wrapper.fadeIn(5000); });
dfr.then(function() { return $.get('/detail.html'}) });
dfr.done(function() { console.log('All done, run additional animations...'); });
dfr.resolve();
... but that just executes all of the then
/done
calls simultaneously. I also tried switching in pipe
in place of the then
calls, to no avail.
I would love to understand Deferred Objects (is this even an appropriate/intended use for them..?) and, of course, how I can achieve my goals for this page. Any help or tips are much appreciated....