Skip to content

Instantly share code, notes, and snippets.

@jgable
Created October 18, 2011 05:33
Show Gist options
  • Save jgable/1294687 to your computer and use it in GitHub Desktop.
Save jgable/1294687 to your computer and use it in GitHub Desktop.
Modern Web Development Blog Post code - Module Export Pattern
// Initialize our apps namespace; add a pages holder for later.
var MYAPP = {
pages: {}
};
// A closure to encapsulate our HomePage class.
(function($, app) {
// This little guy is private to this closure.
function bindButtons($page) {
$page.find("#shiny").click(function() {
// Do something on shiny button click.
});
}
// We will export this class below, but it's private to this closure.
function HomePage(page) {
this.$page = $(page);
bindButtons(this.$page);
}
// Export the HomePage class to the MYAPP.pages namespace
app.pages = $.extend(app.pages, {
Home: HomePage
});
}(jQuery, MYAPP));
// We can use our exported class this way somewhere else in our code.
var myHomePage = new MYAPP.HomePage($('#Home'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment