Models
class User extends Spine.Model
@configure "User", "name", "address"
fullName: ->
[@first, @last].join ' '
JavaScript
User = Spine.Model.sub()
Class methods
.configure 'modelname', attributes...
.include(Module)
.extend(Module)
.create(name: "John")
.count()
.on 'refresh change', (user) -> ...
.trigger 'event'
.change (user) -> ...
.fetch (user) -> ...
.toJSON()
.fromJSON(json)
.fromForm(el)
.records
.attributes
.toString()
.exists(1)
.find(1)
.select (u) u.name == 'bob'
.findByAttribute 'name', 'bob'
.findAllByAttribute 'name', 'bob'
.all()
.slice(6, 13)
.each (user) ->
.first()
.last()
.deleteAll()
.destroyAll()
.destroyAll({ ..options.. })
.destroy(2)
Instance methods
user = new User();
user
.isNew()
.exists()
.isValid()
.validate()
.attributes()
.eql(other)
.load(attrs)
.reload()
.fromForm(form)
.updateAttribute("name", "john")
.updateAttributes(name: "John")
.on 'event', -> ...
.trigger 'event'
.toJSON()
.save()
.destroy()
.dup()
Mixins
class User extends Spine.Model
@include MyModule
@extend MyModule
Events
.on 'create'
.on 'update'
.on 'destroy'
.on 'save'
.on 'change'
.on 'refresh'
.on 'error'
Ajax
class User extends Spine.Model
@extend Spine.Model.Ajax
@url: '/users'
@url: -> '/users'
scope: '2013'
Using
User.fetch()
user = new User()
user.url()
user.url('bands')
user.scope = 'admin'
user.url()
Host
Spine.Model.host = 'http://endpoint'
Ajax mapping
read → GET /collection
create → POST /collection (201 created)
update → PUT /collection/id
destroy → DELETE /collection/id
Associations
class Photo extends Spine.Model
@belongsTo 'album', 'Album'
@belongsTo 'album', 'models/album'
class Album
@hasMany 'photos', 'models/photo'
album.photos().all()
album.photos().create(name: "Vacation")
album.photos().find(id)
photo = Photo.create(album: album)
photo.album()
photo.album_id
See
- http://spinejs.com/api/index
- http://spinejs.com/api/models
- http://spinejs.com/docs/ajax
- http://spinejs.com/docs/relations