Fun with the Observer Pattern
With a little help from http://jsfiddle.net/krasimir/sHNKD/
by deanpeters
HTML
<h2>You can see results in your FireBug/Inspector panel</h2>
JavaScript
(function($){
if(!$.miGap){
$.miGap = new Object();
};
// see http://jsfiddle.net/krasimir/sHNKD/
$.miGap.serverObserverMeh = function(el, options){
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("miGap.serverObserver", base);
base.init = function(){
base.options = $.extend({},$.miGap.serverObserver.defaultOptions, options);
// Put your initialization code here
};
base.list = [],
base.listeners = {};
base.publish = function(name) {
console.log("base.listeners:", base.listeners);
$.each(base.listeners, function(key, value) {
/*
if(base.list[key]) {
base.list[key] += 1;
return false; // only breaking loop
} else {
base.list[key] = 1;
base.dispatch(key);
}
*/
console.log(" &&&&&&&&&&&&&&&&&&&&&&& ", key, value);
base.dispatch(key);
});
base.list = [];
};
// to create an unsubscribe, you'd need to add an arg of "subscriberName" ...
base.on = function(eventName, listener) {
if(!base.listeners[eventName]) base.listeners[eventName] = [];
base.listeners[eventName].push(listener);
console.log(">>> <<<< base.listeners[eventName], base.listeners[eventName].length, eventName", base.listeners[eventName],...