Created
October 18, 2011 05:33
-
-
Save jgable/1294687 to your computer and use it in GitHub Desktop.
Modern Web Development Blog Post code - Module Export Pattern
This file contains hidden or 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
// 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