Created
May 11, 2015 15:18
-
-
Save richardcornish/c41acd58d6ff7fd18f07 to your computer and use it in GitHub Desktop.
Boilerplate for JavaScript module pattern http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript
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
// One global variable for all your JavaScript | |
var MY_SITE = window.MY_SITE || {}; | |
// Requires jQuery | |
// Global jQuery object is aliased to avoid polluting global namespace with "$" | |
MY_SITE = (function ($) { | |
'use strict'; | |
var private_function; | |
// Private function | |
// Can only be called by public functions below because of closure | |
private_function = function () { | |
return; | |
}; | |
return { | |
// Public function | |
// Can be called "outside" of this closure on an individual basis | |
// e.g. MY_SITE.public_function() | |
// Or can be bundled in single function meant specifically for page load | |
// e.g. MY_SITE.init() | |
public_function: function () { | |
private_function(); | |
return; | |
}, | |
// Open a new window for external URLs | |
openNewWindow: function () { | |
$('.js-external, a[rel="external"], a[href*=".pdf"]').click(function (event) { | |
window.open($(this).attr('href')); | |
event.preventDefault(); | |
}); | |
}, | |
// Initialize function collects all functions meant for every page load | |
init: function () { | |
var self = this; | |
self.openNewWindow(); | |
} | |
}; | |
}(jQuery)); | |
// Call initialize function ONCE | |
// There is NO NEED to have a dozen disorganized document.ready() functions | |
jQuery(function () { | |
'use strict'; | |
MY_SITE.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment