Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Last active October 26, 2022 20:26
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ericelliott/9b91b2190c68d0083d9058cdb6e3fc59 to your computer and use it in GitHub Desktop.
Save ericelliott/9b91b2190c68d0083d9058cdb6e3fc59 to your computer and use it in GitHub Desktop.
Model mixin
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