Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chemzqm/model
Minimal emitter model
https://github.com/chemzqm/model
Last synced: about 1 month ago
JSON representation
Minimal emitter model
- Host: GitHub
- URL: https://github.com/chemzqm/model
- Owner: chemzqm
- Created: 2015-10-12T12:20:24.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-03-12T19:21:40.000Z (almost 9 years ago)
- Last Synced: 2024-05-21T17:32:22.651Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 104 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: Readme.md
- Changelog: history.md
Awesome Lists containing this project
README
# model
[![NPM version](https://img.shields.io/npm/v/model-component.svg?style=flat-square)](https://www.npmjs.com/package/model-component)
[![Dependency Status](https://img.shields.io/david/chemzqm/model.svg?style=flat-square)](https://david-dm.org/chemzqm/model)
[![Build Status](https://img.shields.io/travis/chemzqm/model/master.svg?style=flat-square)](http://travis-ci.org/chemzqm/model)
[![Coverage Status](https://img.shields.io/coveralls/chemzqm/model/master.svg?style=flat-square)](https://coveralls.io/github/chemzqm/model?branch=master)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](https://github.com/chemzqm/reactive).You may need [polyfill code](https://gist.github.com/chemzqm/22c1cec5895afd924bee) for ie < 9
## Install
```
npm i model-component
```## API
### Model(name)
Factory method, Create a new model with the given `name`.
```js
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
* `change` event emitted on model instance.
``` js
user.on('change name', function(val, prev) {
api.updateUser({name: val, id: user.id}, function(err) {
})
})
```
* `change $stat` event emitted on model stat change``` js
user.on('change $stat', function(isDirty) {
if (isDirty) {
// it's dirty
} else {
// it become clean
}
})```
## Add methods to model
``` js
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