Skip to content

Instantly share code, notes, and snippets.

@sporto
Created February 10, 2012 06:26
Javascript Boilerplate Structure with Closures
//define a global object where to put all our code
//this is an immediate calling function
window.APP = (function(){
//create an object that will be returned
var that = {};
//private methods
function init(){
APP.registrationController.init();
APP.galleryController.init();
};
//public properties
that.foo = true;
//expose public methods
that.init = init;
return that;
}());
//define other controllers as needed
APP.registrationController = (function(){
//same structure as APP
var that = {};
function init(){
//...
}
that.init = init;
return that;
}());
app.galleryController = (function(){
//same
}());
//jquery dom ready
$(function(){
//in here just call APP init and nothing else
APP.init();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment