Home

Awesome

model

NPM version Dependency Status Build Status Coverage Status

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

Install

npm i model-component

API

Model(name)

Factory method, Create a new model with the given name.

var Model = require('model')
var User = Model('User')

Model.attr(name, [meta])

Define an attribute name with optional meta data object (used by plugins).

Model.method(name, fn)

Add function with name to model prototype

.changed([attr])

Return false or an object

containing the "dirty" attributes. Optionally check for a specific attr.

.clean()

Set current attrs as original attrs

.set(attrs)

Set multiple attrs.

.has(attr)

Check if attr is present (not null or undefined).

.use(plugin)

Plugin is called with Model class

Events

user.on('change name', function(val, prev) {
  api.updateUser({name: val, id: user.id}, function(err) {
  })
})
user.on('change $stat', function(isDirty) {
  if (isDirty) {
  // it's dirty
  } else {
  // it become clean
  }
})

Add methods to model

User.method('destroy', function() {
  var self = this
  api.removeUser(this.primary(), function(err) {
    if (err) { return; }
    //remove bindings
    self.off()
  })
})

Testing

using webpack-dev-server

$ npm install webpack webpack-dev-server -g
$ npm install
$ make test

License

MIT