Quick & dirty jQuery.ready like functionality
var MYAPP = {};
(function(app) {
var readyList, isReady = false;
function readyEvent() {
isReady = true;
console.log( 'readyEvent started' );
readyList.resolve();
};
function getPromise(obj) {
if( !readyList ) {
console.log( 'readyList created');
readyList = $.Deferred();
// If the object is in ready state, execute ready event
if( this.isReady ) {
setTimeout(readyEvent);
}
}
return readyList.promise(obj);
}
function start() {
console.log( 'app started' );
// start async stuff here
// simulated with a setTimeout
setTimeout(readyEvent, 3000);
};
jQuery.extend(app, {
ready: function(fn){
console.log( 'function added to list', fn);
getPromise().done(fn);
}
});
console.log( app, MYAPP);
start();
}(MYAPP));
MYAPP.ready(function(){
console.log( 'First function executed');
});
MYAPP.ready(function(){
console.log( 'Second function executed');
});