OSDN Git Service

Regular updates
[twpd/master.git] / spine.md
1 ---
2 title: Spine
3 category: JavaScript libraries
4 vim: ft=python
5 ---
6
7 ## Models
8
9     class User extends Spine.Model
10       @configure "User", "name", "address"
11
12       fullName: ->
13         [@first, @last].join ' '
14
15 ### JavaScript
16
17     // Subclassing
18     User = Spine.Model.sub()
19
20 ### Class methods
21
22     .configure 'modelname', attributes...
23
24     # Inheritance
25     .include(Module)
26     .extend(Module)
27
28     .create(name: "John")
29
30     .count()
31
32     # Events
33     .on 'refresh change', (user) -> ...
34     .trigger 'event'
35
36     .change (user) -> ...  # same as on('change')
37     .fetch (user) -> ...   # same as on('fetch')
38
39     # JSON
40     .toJSON()         # all records
41     .fromJSON(json)   # from json string
42     .fromForm(el)
43
44     # Data
45     .records     # Hash of instances
46     .attributes  # array of attributes (from .configure)
47
48     # Convenience
49     .toString()  #=> "User"
50
51     # Find by ID
52     .exists(1)
53     .find(1)     # throws error
54
55     # Find by something
56     .select (u) u.name == 'bob'
57     .findByAttribute 'name', 'bob'
58     .findAllByAttribute 'name', 'bob'
59
60     .all()
61     .slice(6, 13)  # cloned copies of instances
62
63     # Iterating
64     .each (user) ->
65
66     # Ends
67     .first()
68     .last()
69
70     # Deleting
71     .deleteAll()
72     .destroyAll()
73     .destroyAll({ ..options.. })
74     .destroy(2)
75
76 ### Instance methods
77
78     user = new User();
79
80     user
81     .isNew()
82     .exists()
83
84     # Validation
85     .isValid()
86     .validate()    # validate = (-> "Name required" unless @name)
87
88     .attributes()  # hash of attr values
89     .eql(other)    # equality check
90
91     # Update
92     .load(attrs)
93     .reload()
94     .fromForm(form)
95     .updateAttribute("name", "john")
96     .updateAttributes(name: "John")
97
98     # Event
99     .on 'event', -> ...
100     .trigger 'event'
101
102     # Retrieve
103     .toJSON()
104
105     # Persistence
106     .save()
107
108     .destroy()
109     .dup()         # clone as unsaved
110
111 ### Mixins
112
113     class User extends Spine.Model
114       @include MyModule
115       @extend MyModule
116
117 ### Events
118
119     .on 'create'
120     .on 'update'
121     .on 'destroy'
122
123     .on 'save'    # create / update
124     .on 'change'  # create / update / destroy
125
126     .on 'refresh'
127     .on 'error'    # validation error
128
129 ## Ajax
130
131     class User extends Spine.Model
132       @extend Spine.Model.Ajax
133
134       @url: '/users'
135       @url: -> '/users'
136       scope: '2013'
137
138 ### Using
139
140     User.fetch()
141     user = new User()
142
143     user.url()            #=> "/users"
144     user.url('bands')     #=> "/users/bands"
145
146     user.scope = 'admin'
147     user.url()            #=> "/admin/users"
148
149 ### Host
150
151     Spine.Model.host = 'http://endpoint'
152
153 ### Ajax mapping
154
155     read    → GET    /collection
156     create  → POST   /collection (201 created)
157     update  → PUT    /collection/id
158     destroy → DELETE /collection/id
159
160 ### Associations
161
162     class Photo extends Spine.Model
163       @belongsTo 'album', 'Album'          # window['Album']
164       @belongsTo 'album', 'models/album'   # via require.js
165
166     class Album
167       @hasMany 'photos', 'models/photo'
168
169     album.photos().all()
170     album.photos().create(name: "Vacation")
171     album.photos().find(id)
172
173     photo = Photo.create(album: album)
174     photo.album()
175     photo.album_id
176
177
178 ### See
179
180  * http://spinejs.com/api/index
181  * http://spinejs.com/api/models
182  * http://spinejs.com/docs/ajax
183 * http://spinejs.com/docs/relations