Last active
April 16, 2017 13:05
-
-
Save Wachiwi/77b8932561ad975aeb2556cadf9ddc61 to your computer and use it in GitHub Desktop.
Javascript event-driven data object
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 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