3 category: JavaScript libraries
9 class User extends Spine.Model
10 @configure "User", "name", "address"
13 [@first, @last].join ' '
18 User = Spine.Model.sub()
22 .configure 'modelname', attributes...
33 .on 'refresh change', (user) -> ...
36 .change (user) -> ... # same as on('change')
37 .fetch (user) -> ... # same as on('fetch')
40 .toJSON() # all records
41 .fromJSON(json) # from json string
45 .records # Hash of instances
46 .attributes # array of attributes (from .configure)
49 .toString() #=> "User"
53 .find(1) # throws error
56 .select (u) u.name == 'bob'
57 .findByAttribute 'name', 'bob'
58 .findAllByAttribute 'name', 'bob'
61 .slice(6, 13) # cloned copies of instances
73 .destroyAll({ ..options.. })
86 .validate() # validate = (-> "Name required" unless @name)
88 .attributes() # hash of attr values
89 .eql(other) # equality check
95 .updateAttribute("name", "john")
96 .updateAttributes(name: "John")
109 .dup() # clone as unsaved
113 class User extends Spine.Model
123 .on 'save' # create / update
124 .on 'change' # create / update / destroy
127 .on 'error' # validation error
131 class User extends Spine.Model
132 @extend Spine.Model.Ajax
143 user.url() #=> "/users"
144 user.url('bands') #=> "/users/bands"
147 user.url() #=> "/admin/users"
151 Spine.Model.host = 'http://endpoint'
155 read → GET /collection
156 create → POST /collection (201 created)
157 update → PUT /collection/id
158 destroy → DELETE /collection/id
162 class Photo extends Spine.Model
163 @belongsTo 'album', 'Album' # window['Album']
164 @belongsTo 'album', 'models/album' # via require.js
167 @hasMany 'photos', 'models/photo'
170 album.photos().create(name: "Vacation")
171 album.photos().find(id)
173 photo = Photo.create(album: album)
180 * http://spinejs.com/api/index
181 * http://spinejs.com/api/models
182 * http://spinejs.com/docs/ajax
183 * http://spinejs.com/docs/relations