Skip to content

Instantly share code, notes, and snippets.

@feynon
Created August 24, 2019 06:28
Show Gist options
  • Save feynon/8f661921bcd3ce662a553329e9aae696 to your computer and use it in GitHub Desktop.
Save feynon/8f661921bcd3ce662a553329e9aae696 to your computer and use it in GitHub Desktop.
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