/ Published in: JavaScript
Could be improved by passing an event instead of a simple payload.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** origin: http://blog.rvi-media.de/javascript/observer-in-javascript/ */ var Observer = function() { this.listeners = []; this.add = function(callback) { this.listeners.push(callback); }; this.remove = function(callback) { /* TODO! ;) */ }; this.update = function(payload) { for (i in this.listeners) this.listeners[i](payload); }; }; var o = new Observer(); o.add(function(payload){document.writeln(payload);}); o.add(function(payload){alert(payload);}); o.update('foo'); o.update('bar');
URL: http://blog.rvi-media.de/javascript/observer-in-javascript/