forked from platanus/angular-restmod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord-api.js
More file actions
406 lines (372 loc) · 11.8 KB
/
Copy pathrecord-api.js
File metadata and controls
406 lines (372 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
'use strict';
RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
/**
* @class RelationScope
*
* @description
*
* Special scope a record provides to resources related via hasMany or hasOne relation.
*/
var RelationScope = function(_scope, _target, _partial) {
this.$scope = _scope;
this.$target = _target;
this.$partial = _partial;
};
RelationScope.prototype = {
// nest collection url
$url: function() {
return Utils.joinUrl(this.$scope.$url(), this.$partial);
},
// record url is nested only for anonymous resources
$urlFor: function(_pk) {
if(this.$target.$anonymous()) {
return this.$fetchUrlFor();
} else {
return this.$target.$urlFor(_pk);
}
},
// fetch url is nested
$fetchUrlFor: function(/* _pk */) {
return Utils.joinUrl(this.$scope.$url(), this.$partial);
},
// create is not posible in nested members
$createUrlFor: function() {
return null;
}
};
/**
* @class RecordApi
* @extends CommonApi
*
* @property {object} $scope The record's scope (see {@link ScopeApi})
* @property {mixed} $pk The record's primary key
*
* @description
*
* Provides record synchronization and manipulation methods. This is the base API for every restmod record.
*
* TODO: Talk about the object lifecycle.
*
* ### Object lifecycle hooks
*
* For `$fetch`:
*
* * before-fetch
* * before-request
* * after-request[-error]
* * after-feed (only called if no errors)
* * after-fetch[-error]
*
* For `$save` when creating:
*
* * before-render
* * before-save
* * before-create
* * before-request
* * after-request[-error]
* * after-feed (only called if no errors)
* * after-create[-error]
* * after-save[-error]
*
* For `$save` when updating:
*
* * before-render
* * before-save
* * before-update
* * before-request
* * after-request[-error]
* * after-feed (only called if no errors)
* * after-update[-error]
* * after-save[-error]
*
* For `$destroy`:
*
* * before-destroy
* * before-request
* * after-request[-error]
* * after-destroy[-error]
*
* @property {mixed} $pk The record primary key
* @property {object} $scope The collection scope (hierarchical scope, not angular scope)
*/
return {
/**
* @memberof RecordApi#
*
* @description Called by record constructor on initialization.
*
* Note: Is better to add a hook to after-init than overriding this method.
*/
$initialize: function() {
// apply defaults
this.$$loadDefaults();
// after initialization hook
// TODO: put this on $new so it can use stacked DSP?
this.$dispatch('after-init');
},
/**
* @memberof RecordApi#
*
* @description Returns the url this object is bound to.
*
* This is the url used by fetch to retrieve the resource related data.
*
* @return {string} bound url.
*/
$url: function() {
return this.$scope.$urlFor(this.$pk);
},
/**
* @memberof RecordApi#
*
* @description Default item child scope factory.
*
* By default, no create url is provided and the update/destroy url providers
* attempt to first use the unscoped resource url.
*
* // TODO: create special api to hold scope (so it is not necessary to recreate the whole object every time.)
*
* @param {mixed} _for Scope target type, accepts any model class.
* @param {string} _partial Partial route.
* @return {RelationScope} New scope.
*/
$buildScope: function(_for, _partial) {
if(_for.$buildOwnScope) {
// TODO
} else {
return new RelationScope(this, _for, _partial);
}
},
/**
* @memberof RecordApi#
*
* @description Copyies another object's non-private properties.
*
* @param {object} _other Object to merge.
* @return {RecordApi} self
*/
$extend: function(_other) {
for(var tmp in _other) {
if (_other.hasOwnProperty(tmp) && tmp[0] !== '$') {
this[tmp] = _other[tmp];
}
}
return this;
},
/**
* @memberof RecordApi#
*
* @description Iterates over the object non-private properties
*
* @param {function} _fun Function to call for each
* @return {RecordApi} self
*/
$each: function(_fun, _ctx) {
for(var key in this) {
if(this.hasOwnProperty(key) && key[0] !== '$') {
_fun.call(_ctx || this[key], this[key], key);
}
}
return this;
},
/**
* @memberof RecordApi#
*
* @description Feed raw data to this instance.
*
* @param {object} _raw Raw data to be fed
* @param {string} _mask 'CRU' mask
* @return {RecordApi} this
*/
$decode: function(_raw, _mask) {
this.$$transform(_raw, '', _mask || Utils.READ_MASK, true, this);
if(!this.$pk) this.$pk = this.$$inferKey(_raw); // TODO: warn if key changes
this.$dispatch('after-feed', [_raw]);
return this;
},
/**
* @memberof RecordApi#
*
* @description Generate data to be sent to the server when creating/updating the resource.
*
* @param {string} _mask 'CRU' mask
* @return {string} raw data
*/
$encode: function(_mask) {
var raw = this.$$transform(this, '', _mask || Utils.CREATE_MASK, false);
this.$dispatch('before-render', [raw]);
return raw;
},
/**
* @memberof RecordApi#
*
* @description
*
* Unpacks and decode raw data from a server generated structure.
*
* ATTENTION: do not override this method to change the object wrapping strategy,
* instead, check {@link BuilderApi#setPacker} for instruction about loading a new packer.
*
* @param {mixed} _raw Raw server data
* @param {string} _mask 'CRU' mask
* @return {RecordApi} this
*/
$unwrap: function(_raw, _mask) {
_raw = this.$$unpack(_raw);
return this.$decode(_raw, _mask);
},
/**
* @memberof RecordApi#
*
* @description
*
* Encode and packs object into a server compatible structure that can be used for PUT/POST operations.
*
* ATTENTION: do not override this method to change the object wrapping strategy,
* instead, check {@link BuilderApi#setPacker} for instruction about loading a new packer.
*
* @param {string} _mask 'CRU' mask
* @return {string} raw data
*/
$wrap: function(_mask) {
var raw = this.$encode(_mask);
raw = this.$$pack(raw);
return raw;
},
/**
* @memberof RecordApi#
*
* @description Begin a server request for updated resource data.
*
* The request's promise is provided as the $promise property.
*
* @param {object} _params Optional list of params to be passed to object request.
* @return {RecordApi} this
*/
$fetch: function(_params) {
// verify that instance has a bound url
var url = this.$scope.$fetchUrlFor ? this.$scope.$fetchUrlFor(this.$pk) : this.$url();
if(!url) throw new Error('Cannot fetch an unbound resource');
var request = { method: 'GET', url: url, params: _params };
this.$dispatch('before-fetch', [request]);
return this.$send(request, function(_response) {
this.$unwrap(_response.data);
this.$dispatch('after-fetch', [_response]);
}, function(_response) {
this.$dispatch('after-fetch-error', [_response]);
});
},
/**
* @memberof RecordApi#
*
* @description Begin a server request to create/update resource.
*
* If resource is new and it belongs to a collection and it hasnt been revealed, then it will be revealed.
*
* The request's promise is provided as the $promise property.
*
* @return {RecordApi} this
*/
$save: function() {
var url = this.$scope.$updateUrlFor ? this.$scope.$updateUrlFor(this.$pk) : this.$url(), request;
if(url) {
// If bound, update
request = { method: 'PUT', url: url, data: this.$wrap(Utils.UPDATE_MASK) };
this.$dispatch('before-update', [request]);
this.$dispatch('before-save', [request]);
return this.$send(request, function(_response) {
this.$unwrap(_response.data);
this.$dispatch('after-update', [_response]);
this.$dispatch('after-save', [_response]);
}, function(_response) {
this.$dispatch('after-update-error', [_response]);
this.$dispatch('after-save-error', [_response]);
});
} else {
// If not bound create.
url = this.$scope.$createUrlFor ? this.$scope.$createUrlFor(this.$pk) : (this.$scope.$url && this.$scope.$url());
if(!url) throw new Error('Create is not supported by this resource');
request = { method: 'POST', url: url, data: this.$wrap(Utils.CREATE_MASK) };
this.$dispatch('before-save', [request]);
this.$dispatch('before-create', [request]);
return this.$send(request, function(_response) {
this.$unwrap(_response.data);
// reveal item (if not yet positioned)
if(this.$scope.$isCollection && this.$position === undefined && !this.$preventReveal) {
this.$scope.$add(this, this.$revealAt);
}
this.$dispatch('after-create', [_response]);
this.$dispatch('after-save', [_response]);
}, function(_response) {
this.$dispatch('after-create-error', [_response]);
this.$dispatch('after-save-error', [_response]);
});
}
},
/**
* @memberof RecordApi#
*
* @description Begin a server request to destroy the resource.
*
* The request's promise is provided as the $promise property.
*
* @return {RecordApi} this
*/
$destroy: function() {
var url = this.$scope.$destroyUrlFor ? this.$scope.$destroyUrlFor(this.$pk) : this.$url();
if(!url) throw new Error('Cannot destroy an unbound resource');
var request = { method: 'DELETE', url: url };
this.$dispatch('before-destroy', [request]);
return this.$send(request, function(_response) {
// call scope callback
if(this.$scope.$remove) {
this.$scope.$remove(this);
}
this.$dispatch('after-destroy', [_response]);
}, function(_response) {
this.$dispatch('after-destroy-error', [_response]);
});
},
// Collection related methods.
/**
* @memberof RecordApi#
*
* @description Changes the location of the object in the bound collection.
*
* If object hasn't been revealed, then this method will change the index where object will be revealed at.
*
* @param {integer} _to New object position (index)
* @return {RecordApi} this
*/
$moveTo: function(_to) {
if(this.$position !== undefined) {
// TODO: move item to given index.
// TODO: callback
} else {
this.$revealAt = _to;
}
return this;
},
/**
* @memberof RecordApi#
*
* @description Reveal in collection
*
* If instance is bound to a collection and it hasnt been revealed (because it's new and hasn't been saved),
* then calling this method without parameters will force the object to be added to the collection.
*
* If this method is called with **_show** set to `false`, then the object wont be revealed by a save operation.
*
* @param {boolean} _show Whether to reveal inmediatelly or prevent automatic reveal.
* @return {RecordApi} this
*/
$reveal: function(_show) {
if(_show === undefined || _show) {
this.$scope.$add(this, this.$revealAt);
} else {
this.$preventReveal = true;
}
return this;
}
};
}]);