Skip to content

Instantly share code, notes, and snippets.

@Wachiwi
Last active April 16, 2017 13:05
Show Gist options
  • Save Wachiwi/77b8932561ad975aeb2556cadf9ddc61 to your computer and use it in GitHub Desktop.
Save Wachiwi/77b8932561ad975aeb2556cadf9ddc61 to your computer and use it in GitHub Desktop.
Javascript event-driven data object
var data = {
_value: undefined,
_handlers: [],
set: function(val, cb){
this._value = val;
this.attach(cb);
this._handlers.forEach(function(cb){cb();});
},
get: function() {return this._value;},
attach: function(cb) {
if (cb && this._handlers.indexOf(cb) === -1) {
this._handlers.push(cb);
}
},
detach: function(cb) {
if (cb && this._handlers.indexOf(cb) !== 0) {
this._handlers.splice(this._handlers.indexOf(cb),1);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment