cheatsheets/modella.md

128 lines
1.9 KiB
Markdown
Raw Permalink Normal View History

2014-02-25 10:32:14 +00:00
---
title: Modella
2015-11-24 05:02:17 +00:00
category: JavaScript libraries
2017-10-11 07:50:19 +00:00
prism_languages: [coffeescript]
intro: |
[Modella](https://www.npmjs.com/package/modella) allows you to create simple models in JavaScript. This is a guide on basic usage of Modella in CoffeeScript.
2014-02-25 10:32:14 +00:00
---
2017-10-11 07:50:19 +00:00
### Defining models
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
User = Modella('User')
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
.attr('name')
.attr('email', { required: true })
.use(require('modella-validators'))
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
.validator (u) ->
u.error('username', 'is required') unless u.has('username')
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
### Instances
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
user
.name()
.name('John')
.set(name: 'John')
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
.has('name') # → true
.isNew()
.isValid()
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
.save (err) ->
.remove (err) ->
.removed
.model # === User
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
## Events
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
### Emitting
```coffeescript
Model.emit('event', [data...])
2017-10-11 07:51:59 +00:00
```
```coffeescript
2017-10-11 07:50:19 +00:00
record.emit('event', [data...])
```
2014-02-25 10:32:14 +00:00
### List of events
2017-10-11 07:50:19 +00:00
```coffeescript
user
.on 'save', ->
.on 'create', ->
.on 'saving', (data, done) -> done()
```
```coffeescript
.on 'remove', ->
.on 'removing', (data, done) -> done()
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
.on 'valid', ->
.on 'invalid', ->
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
.on 'change', ->
.on 'change email', ->
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
.on 'initializing', (instance, attrs) ->
.on 'initialize', ->
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
.on 'error', -> failed to save model
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
```coffeescript
.on 'setting', (instance, attrs) -> # on Model#set()
.on 'attr', -> # new attr via Model.attr()
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
## Misc
2014-02-25 10:32:14 +00:00
### Plugins
2017-10-11 07:50:19 +00:00
```coffeescript
MyPlugin = ->
return (Model) ->
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
Model.method = ...
Model.prototype.method = ...
Model.attr(...)
2014-02-25 10:32:14 +00:00
2017-10-11 07:50:19 +00:00
Model
```
2014-02-25 10:32:14 +00:00
2017-10-11 07:51:59 +00:00
A plugin is a function that returns a model decorator (ie, a function that takes in a model and returns a model).
2014-02-25 10:32:14 +00:00
### Memory
2017-10-11 07:50:19 +00:00
```coffeescript
User
.all (err, users) ->
.find id, (err, user) ->
```
```coffeescript
.remove ->
.save ->
.update ->
```