2
2
import DSUtils from '../../utils' ;
3
3
import DSErrors from '../../errors' ;
4
4
5
+ /**
6
+ * These are DS methods that will be proxied by instances. e.g.
7
+ *
8
+ * var store = new JSData.DS();
9
+ * var User = store.defineResource('user');
10
+ * var user = User.createInstance({ id: 1 });
11
+ *
12
+ * store.update(resourceName, id, attrs[, options]) // DS method
13
+ * User.update(id, attrs[, options]) // DS method proxied on a Resource
14
+ * user.DSUpdate(attrs[, options]) // DS method proxied on an Instance
15
+ */
5
16
let instanceMethods = [
6
17
'compute' ,
18
+ 'eject' ,
7
19
'refresh' ,
8
20
'save' ,
9
21
'update' ,
@@ -21,6 +33,10 @@ export default function defineResource(definition) {
21
33
let _this = this ;
22
34
let definitions = _this . defs ;
23
35
36
+ /**
37
+ * This allows the name-only definition shorthand.
38
+ * store.defineResource('user') is the same as store.defineResource({ name: 'user'})
39
+ */
24
40
if ( DSUtils . _s ( definition ) ) {
25
41
definition = {
26
42
name : definition . replace ( / \s / gi, '' )
@@ -34,6 +50,11 @@ export default function defineResource(definition) {
34
50
throw new DSErrors . R ( `${ definition . name } is already registered!` ) ;
35
51
}
36
52
53
+ /**
54
+ * Dynamic Resource constructor function.
55
+ *
56
+ * A Resource inherits from the defaults of the data store that created it.
57
+ */
37
58
function Resource ( options ) {
38
59
this . defaultValues = { } ;
39
60
this . methods = { } ;
@@ -50,6 +71,7 @@ export default function defineResource(definition) {
50
71
}
51
72
52
73
try {
74
+ // Resources can inherit from another resource instead of inheriting directly from the data store defaults.
53
75
if ( definition . extends && definitions [ definition . extends ] ) {
54
76
// Inherit from another resource
55
77
Resource . prototype = definitions [ definition . extends ] ;
@@ -61,9 +83,6 @@ export default function defineResource(definition) {
61
83
62
84
var def = definitions [ definition . name ] ;
63
85
64
- // alias name, shaves 0.08 kb off the minified build
65
- def . n = def . name ;
66
-
67
86
def . logFn ( 'Preparing resource.' ) ;
68
87
69
88
if ( ! DSUtils . _s ( def . idAttribute ) ) {
@@ -82,7 +101,7 @@ export default function defineResource(definition) {
82
101
DSUtils . forEach ( relatedModels [ relationName ] , d => {
83
102
d . type = type ;
84
103
d . relation = relationName ;
85
- d . name = def . n ;
104
+ d . name = def . name ;
86
105
def . relationList . push ( d ) ;
87
106
if ( d . localField ) {
88
107
def . relationFields . push ( d . localField ) ;
@@ -153,13 +172,6 @@ export default function defineResource(definition) {
153
172
}
154
173
} ;
155
174
156
- // Remove this in v0.11.0 and make a breaking change notice
157
- // the the `filter` option has been renamed to `defaultFilter`
158
- if ( def . filter ) {
159
- def . defaultFilter = def . filter ;
160
- delete def . filter ;
161
- }
162
-
163
175
// Create the wrapper class for the new resource
164
176
var _class = def [ 'class' ] = DSUtils . pascalCase ( def . name ) ;
165
177
try {
@@ -182,21 +194,30 @@ export default function defineResource(definition) {
182
194
} ;
183
195
}
184
196
185
- // Apply developer-defined methods
197
+ // Apply developer-defined instance methods
186
198
DSUtils . forOwn ( def . methods , ( fn , m ) => {
187
199
def [ _class ] . prototype [ m ] = fn ;
188
200
} ) ;
189
201
202
+ /**
203
+ * var user = User.createInstance({ id: 1 });
204
+ * user.set('foo', 'bar');
205
+ */
190
206
def [ _class ] . prototype . set = function ( key , value ) {
191
207
DSUtils . set ( this , key , value ) ;
192
- _this . compute ( def . n , this ) ;
208
+ _this . compute ( def . name , this ) ;
193
209
return this ;
194
210
} ;
195
211
212
+ /**
213
+ * var user = User.createInstance({ id: 1 });
214
+ * user.get('id'); // 1
215
+ */
196
216
def [ _class ] . prototype . get = function ( key ) {
197
217
return DSUtils . get ( this , key ) ;
198
218
} ;
199
219
220
+ // Setup the relation links
200
221
DSUtils . applyRelationGettersToTarget ( _this , def , def [ _class ] . prototype ) ;
201
222
202
223
// Prepare for computed properties
@@ -227,22 +248,24 @@ export default function defineResource(definition) {
227
248
} ) ;
228
249
} ) ;
229
250
251
+ // add instance proxies of DS methods
230
252
DSUtils . forEach ( instanceMethods , name => {
231
253
def [ _class ] . prototype [ `DS${ DSUtils . pascalCase ( name ) } ` ] = function ( ...args ) {
232
254
args . unshift ( this [ def . idAttribute ] || this ) ;
233
- args . unshift ( def . n ) ;
255
+ args . unshift ( def . name ) ;
234
256
return _this [ name ] . apply ( _this , args ) ;
235
257
} ;
236
258
} ) ;
237
259
260
+ // manually add instance proxy for DS#create
238
261
def [ _class ] . prototype . DSCreate = function ( ...args ) {
239
262
args . unshift ( this ) ;
240
- args . unshift ( def . n ) ;
263
+ args . unshift ( def . name ) ;
241
264
return _this . create . apply ( _this , args ) ;
242
265
} ;
243
266
244
267
// Initialize store data for the new resource
245
- _this . s [ def . n ] = {
268
+ _this . s [ def . name ] = {
246
269
collection : [ ] ,
247
270
expiresHeap : new DSUtils . BinaryHeap ( x => x . expires , ( x , y ) => x . item === y ) ,
248
271
completedQueries : { } ,
@@ -258,23 +281,33 @@ export default function defineResource(definition) {
258
281
collectionModified : 0
259
282
} ;
260
283
284
+ // start the reaping
261
285
if ( def . reapInterval ) {
262
- setInterval ( ( ) => _this . reap ( def . n , { isInterval : true } ) , def . reapInterval ) ;
286
+ setInterval ( ( ) => _this . reap ( def . name , { isInterval : true } ) , def . reapInterval ) ;
263
287
}
264
288
265
- // Proxy DS methods with shorthand ones
289
+ // proxy DS methods with shorthand ones
10000
266
290
let fns = [ 'registerAdapter' , 'getAdapter' , 'is' ] ;
267
291
for ( var key in _this ) {
268
292
if ( typeof _this [ key ] === 'function' ) {
269
293
fns . push ( key ) ;
270
294
}
271
295
}
272
296
297
+ /**
298
+ * Create the Resource shorthands that proxy DS methods. e.g.
299
+ *
300
+ * var store = new JSData.DS();
301
+ * var User = store.defineResource('user');
302
+ *
303
+ * store.update(resourceName, id, attrs[, options]) // DS method
304
+ * User.update(id, attrs[, options]) // DS method proxied on a Resource
305
+ */
273
306
DSUtils . forEach ( fns , key => {
274
307
let k = key ;
275
308
if ( _this [ k ] . shorthand !== false ) {
276
309
def [ k ] = ( ...args ) => {
277
- args . unshift ( def . n ) ;
310
+ args . unshift ( def . name ) ;
278
311
return _this [ k ] . apply ( _this , args ) ;
279
312
} ;
280
313
def [ k ] . before = fn => {
@@ -302,6 +335,8 @@ export default function defineResource(definition) {
302
335
if ( def . hasOwnProperty ( 'defaultAdapter' ) ) {
303
336
defaultAdapter = def . defaultAdapter ;
304
337
}
338
+
339
+ // setup "actions"
305
340
DSUtils . forOwn ( def . actions , ( action , name ) => {
306
341
if ( def [ name ] && ! def . actions [ name ] ) {
307
342
throw new Error ( `Cannot override existing method "${ name } "!` ) ;
@@ -338,7 +373,7 @@ export default function defineResource(definition) {
338
373
} ;
339
374
} ) ;
340
375
341
- // Mix- in events
376
+ // mix in events
342
377
DSUtils . Events ( def ) ;
343
378
344
379
def . logFn ( 'Done preparing resource.' ) ;
0 commit comments