Last active
September 8, 2019 21:52
-
-
Save Pushplaybang/39ce46ec5b3d5502bb0c to your computer and use it in GitHub Desktop.
Pattern Template : Revealing Module
This file contains 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
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