Last active
March 2, 2019 17:02
-
-
Save marcwiest/d193ecc8cdbd6f673399ee209fa51fc7 to your computer and use it in GitHub Desktop.
jQuery Literal Module 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
| (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