Skip to content

Instantly share code, notes, and snippets.

@redroot
Created May 4, 2014 23:18
Show Gist options
  • Select an option

  • Save redroot/3c7948ec92ecbd3bdf1d to your computer and use it in GitHub Desktop.

Select an option

Save redroot/3c7948ec92ecbd3bdf1d to your computer and use it in GitHub Desktop.
Example JS Module pattern
var Header = (function(w,d,undefined)){
var _elem = null;
var bindButtons = function(){
$(document).on('click', _elem.find(".js-toggle-sidebar"), function(){
Sidebar.toggle(); //
});
}
var init = function(){
_elem = $("#header");
bindButtons();
}
return {
init: init
}
})(window,document);
var Sidebar = (function(w,d,undefined){
var _elem = null;
var hide = function(){
_elem.addClass('is-hidden');
}
var show = function(){
_elem.removeClass('is-hidden')
}
var init = function(){
_elem = $("#sidebar");
}
var toggleSidebar = function(){
elem.hasClass('is-hidden') ? show() hide();
}
// code for all other functions
return {
init: init,
hide: hide,
show: show,
toggle: toggle
// more here
}
})(window,document);
window.onload = function(){
Header.init();
Sidebar.init();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment