Created
June 27, 2011 16:12
-
-
Save ehynds/1049181 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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