/** Observer Design Pattern implementation (similar to Java Observable class : http://java.sun.com/j2se/1.4.2/docs/api/java/util/Observable.html ) @see http://en.wikipedia.org/wiki/Observer_design_pattern */ function Observable() { // Map> // where EventType = "onclick", "onkeydown', .. this.observers = new Object(); /* @visibility public */ this.addHandler = function(event, handler) { var list = this.observers[event]; if(list == null) { list = new Array(); this.observers[event] = list; } list.push(handler); } /* @param data : optional callback data (provided by the notifier) @visibility public */ this.notifyHandlers = function (event, data) { var list = this.observers[event]; if(list != null) { for(var i = 0 ; i < list.length ; i++) { var f = list[i]; if(data != null) { f(data); } else { f(); } } } } /* public */ this.clearHandlers = function(event) { this.observers[event] = null; } }