A plugin that adds change events to models.
$ component install component/model-change
var model = require('model');
var change = require('model-change');
/**
* User model.
*/
var User = model()
.use(change())
.attr('id')
.attr('name');
/**
* Listen.
*/
var user = new User({ name: 'Fred' });
user.on('change name', function (attr, val, prev) {
console.log('name changed from "' + prev + '" to "' + val + '"');
});
user.name('George');
Return the plugin with optional options
:
separator: ' '
Once applied, these events are emitted on the constructor:
Model.emit('change', instance, attr, val, prev);
Model.emit('change<separator><attr>', instance, val, prev)
And these events are emitted on the instance:
model.emit('change', attr, val, prev);
model.emit('change<separator><attr>', val, prev);
Check whether an attr
has changed since the last save.
When no attr
is passed, the method either returns a dictionary of all the attributes that have changed, or false
if none have changed.