Skip to content

Instantly share code, notes, and snippets.

@Pushplaybang
Last active September 8, 2019 21:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pushplaybang/39ce46ec5b3d5502bb0c to your computer and use it in GitHub Desktop.
Save Pushplaybang/39ce46ec5b3d5502bb0c to your computer and use it in GitHub Desktop.
Pattern Template : Revealing Module
var Module = (function(w, $) {
var State = {
initialized : false,
listening : false,
};
var Events = $({});
var Internal = {
/* Throttle Events */
throttle : function(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last, deferTimer;
return function () {
var context = scope || this;
var now = +new Date;
var args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
},
}; // end 'Internal'
/* Public */
var Obj = {
settings : {},
getState : function(param) {
if (param) {
return State[param];
}
console.error("no param set");
return false;
},
setState : function(param, value) {
if (param) {
State[param] = value || State[param];
return State[param];
}
console.error("no param selected");
return false;
},
logState : function(param) {
if (param) {
console.log(State[param]);
return self;
}
console.log(State);
return this;
},
init : function(config, cb) {
if (config && typeof(config) === 'object') {
$.extend( Obj.settings, config); // extend settings
}
this.setState('initialized', true);
this.listen();
if (cb && typeof(cb) === 'function') { cb(); }
return Obj;
},
listen : function(cb) {
Obj.setState('listening', true);
$(".selector a").on('click', Obj.eventCallback);
if (cb && typeof(cb) === 'function') { cb(); }
return Obj;
},
// Method called from event handler in Listen Method.
eventCallback : function(e) {
e.preventDefault();
Events.trigger('custom');
// do stuff
return Obj;
},
// Example Public Method
method : function(cb) {
// do stuff
if (cb && typeof(cb) === 'function') { cb(); }
return Obj;
}
};
return Obj;
})(window, jQuery); // end Module - call init in Doc Ready
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment