OSDN Git Service

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