Skip to content

Instantly share code, notes, and snippets.

@bookwyrm
Last active December 28, 2015 21:19
Show Gist options
  • Save bookwyrm/7564094 to your computer and use it in GitHub Desktop.
Save bookwyrm/7564094 to your computer and use it in GitHub Desktop.
Example of how to organize JavaScript code for small-scale websites.
(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