Skip to content

Instantly share code, notes, and snippets.

@ehynds
Created June 27, 2011 16:12
Show Gist options
  • Save ehynds/1049181 to your computer and use it in GitHub Desktop.
Save ehynds/1049181 to your computer and use it in GitHub Desktop.
// Pattern for organizing DOM ready & non-DOM-ready dependent
// code within an initialization method. i guess this could be
// useful if initialization must take place inside the <head> tag,
// or you absolutely need to kick off some processes ASAP.
(function(){
var app = app || {};
function domReady( callback, context ){
return function( args ) {
$( $.proxy(callback, context, args) );
}
}
app.component = {
init: function(){
// kick off stuff that doesn't require DOM ready,
// like AJAX, but defer the success function until
// DOM ready fires.
$.get('/echo/html/', domReady(this.render, this));
// init stuff that does require the DOM
$( $.proxy(this.initDomReady, this) );
},
initDomReady: function(){
// other DOM-dependent initialization
},
render: function( response ){
// render view or whatever
}
};
// expose
window.app = app;
})();
// in the head, if you must
app.component.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment