ES5 minimalistic extensible model component.
By using Object.defineProperty
, change
event is automaticaly emited on value set.
this feature can make it works with reactive.
You may need polyfill code for ie < 9
npm i model-component
Factory method, Create a new model with the given name
.
var Model = require('model')
var User = Model('User')
Define an attribute name
with optional meta
data object (used by plugins).
Add function with name to model prototype
Return false
or an object
containing the "dirty" attributes.
Optionally check for a specific attr
.
Set current attrs as original attrs
Set multiple attrs
.
Check if attr
is present (not null
or undefined
).
Plugin is called with Model class
change
event emitted on model instance.
user.on('change name', function(val, prev) {
api.updateUser({name: val, id: user.id}, function(err) {
})
})
change $stat
event emitted on model stat change
user.on('change $stat', function(isDirty) {
if (isDirty) {
// it's dirty
} else {
// it become clean
}
})
User.method('destroy', function() {
var self = this
api.removeUser(this.primary(), function(err) {
if (err) { return; }
//remove bindings
self.off()
})
})
using webpack-dev-server
$ npm install webpack webpack-dev-server -g
$ npm install
$ make test
MIT