Last active
December 28, 2015 21:19
-
-
Save bookwyrm/7564094 to your computer and use it in GitHub Desktop.
Example of how to organize JavaScript code for small-scale websites.
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($, window, document) { | |
var SomeSiteComponent = { | |
init: function() { | |
if ($('.some-class').length > 0) { | |
SomeSiteComponent.setup(); | |
} | |
}, | |
setup: function() { | |
$(document).on('click', '.some-class .some-other-class', SomeSiteComponent.clickHandler); | |
}, | |
clickHandler: function(event) { | |
event.preventDefault(); | |
SomeSiteComponent.actionForClick($(this)); | |
} | |
actionForClick: function($el) { | |
// Do something with the element that was clicked on | |
} | |
}; | |
$(SomeSiteComponent.init); | |
})(jQuery, window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment