Skip to content

Instantly share code, notes, and snippets.

@marcwiest
Last active March 2, 2019 17:02
Show Gist options
  • Select an option

  • Save marcwiest/d193ecc8cdbd6f673399ee209fa51fc7 to your computer and use it in GitHub Desktop.

Select an option

Save marcwiest/d193ecc8cdbd6f673399ee209fa51fc7 to your computer and use it in GitHub Desktop.
jQuery Literal Module Pattern
(function( $ ) {
'use strict';
var $doc = $(document);
var module = {
init : function() {
this.createElements();
this.cacheProps();
this.bindEvents();
},
// Use to create ALL elements that are created via JS.
createElements : function() {
this.$anchor = jQuery( '<a href="#" />' );
},
// Use for ALL variable definitions, unless it makes more sense to difine elsewhere.
cacheProps : function() {
this.$someElement = $('.some-element');
},
// Bind ALL events here unless it makes more sense to defined some of them elsewhere.
bindEvents : function() {
$doc.on( 'click', 'body', jQuery.proxy( this.doAlertHandler, this ) );
},
// Prefix all event handlers with "do".
doAlertHandler : function( e ) {
e.preventDefault();
}
};
$doc.ready( function() {
module.init();
});
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment