I needed to write a method to extend the jQuery empty() function to include a callback. Since I didn't want to mess with namespaces, closures, etc, I instead wrote a regular function to wrap empty().
var emptyWithCallback = function(node, callback) {
console.log('Entered emptyWithCallback()');
if (typeof callback != 'function') {
return false;
}
var doTheCallback = callbackWrapper();
var doTheEmpty = myEmpty();
function callbackWrapper() {
var deferredCallback = $.Deferred();
callback();
deferredCallback.resolve();
return deferredCallback.promise();
}
function myEmpty() {
var deferredEmpty = $.Deferred();
$(node).empty();
deferredEmpty.resolve();
return deferredEmpty.promise();
}
doTheCallback.then(doTheEmpty);
}
I was attempting to use the $.Deferred() in case the callback contained an ajax call, in order to not execute the .empty() until the callback concluded.
Does this code look like it will work? It seems to work on simple callbacks, such as a single alert() or console.log().