I have a website that does something similar. The function for popping, replacing content, and pasting is as follows:
var loadAnAjaxArticle = function (url) { var removeParentArticle = function () { // I don't like doing this, but so far haven't found a // particularly elegant way to replace the article // when calling load(), only append it. $('article').first().html($('article').last().html()); }; var showArticle = function (response, status, xhr) { if (status == "error") { $('article').first().html('<h2>There was an error processing your request.</h2>'); $('article').first().slideDown('slow'); } else { removeParentArticle(); $('article').first().slideDown('slow'); } }; var loadArticle = function () { $('article').first().load(url + ' article', showArticle); }; $('article').first().slideUp('slow', loadArticle); }
Basically, it calls .slideUp
to hide the contents, providing feedback to another function. This function calls .load
to update the content, providing feedback to another function. This function manipulates the DOM a bit to make it look right (it is at an early stage of development, I havenβt made it more elegant yet) and calls .slideDown
to show the contents again.
This is easy to do in this case, because each function used takes a callback. If the functions you use have the same capability, you should be able to chain them that way.
David source share