model
W.I.P minimalistic extensible model component.
API
model(name)
Create a new model with the given name
.
var model = ;var User = ;
.attr(name, [meta])
Define an attribute name
with optional meta
data object.
var model = ; var Post =
With meta data used by plugins:
var model = ; var Post =
.validate(fn)
TODO: validation callback docs
.use(fn)
TODO: plugin docs
.url([path])
Return base url, or url to path
.
User// => "/users" User// => "/users/add"
.route(path)
Set base path for urls.
Note this is defaulted to '/' + modelName.toLowerCase() + 's'
User User// => "/api/u" User// => "/api/u/add"
.headers({header: value})
Sets custom headers for static and method requests on the model.
Userheaders 'X-CSRF-Token': 'some token' 'X-API-Token': 'api token' ;
.ATTR()
"Getter" function generated when Model.attr(name)
is called.
var Post = var post = title: 'Cats' ; posttitle// => "Cats"
.ATTR(value)
"Setter" function generated when Model.attr(name)
is called.
var Post = var post = ; posttitle'Ferrets'posttitle// => "Ferrets"
- Emits "change" event with
(name, value, previousValue)
. - Emits "change ATTR" event with
(value, previousValue)
.
post post
.isNew()
Returns true
if the model is unsaved.
.toJSON()
Return a JSON representation of the model (its attributes).
.has(attr)
Check if attr
is non-null
.
.get(attr)
Get attr
's value.
.set(attrs)
Set multiple attrs
.
user
.changed([attr])
Check if the model is "dirty" and return an object
of changed attributes. Optionally check a specific attr
and return a Boolean
.
.error(attr, msg)
Define error msg
for attr
.
.isValid()
Run validations and check if the model is valid.
user// => false usererrors// => [{ attr: ..., message: ... }]
.url([path])
Return this model's base url or relative to path
:
var user = id: 5 ;user;// => "/users/5/edit"
.save(fn)
Save or update and invoke the given callback fn(err)
.
var user = name: 'Tobi' user
Emits "save" when complete.
.destroy([fn])
Destroy and invoke optional fn(err)
.
Emits "destroy" when successfully deleted.
Links
- Plugins for model
Testing
$ npm install
$ make test &
$ open http://localhost:3000
License
MIT