-
-
Save olivmonnier/a71fc87fa42afc3f59ac1f9137b1579a to your computer and use it in GitHub Desktop.
Model mixin
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
import Events from 'eventemitter3'; | |
const modelMixin = Object.assign({ | |
attrs: {}, | |
set (name, value) { | |
this.attrs[name] = value; | |
this.emit('change', { | |
prop: name, | |
value: value | |
}); | |
}, | |
get (name) { | |
return this.attrs[name]; | |
} | |
}, Events.prototype); | |
const george = { name: 'george' }; | |
const model = Object.assign(george, modelMixin); | |
model.on('change', data => console.log(data)); | |
model.set('name', 'Sam'); | |
/* | |
{ | |
prop: 'name', | |
value: 'Sam' | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment