This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathangular-ra-model.js
477 lines (372 loc) · 13 KB
/
angular-ra-model.js
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
'use strict';
// Source: src/angular-ra-model.js
angular.module('ra.model', ['ra.model.services']);
// Source: src/services/model.js
(function() {
// Snippet taken from http://prototypejs.org
function argumentNames(func) {
var names = func.toString()
.match(/^[\s\(]*function[^(]*\(([^)]*)\)/)[1]
.replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, '')
.replace(/\s+/g, '')
.split(',');
return names.length === 1 && !names[0] ? [] : names;
}
function extend(obj) {
angular.forEach(Array.prototype.slice.call(arguments, 1), function(source) {
if (obj && source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
});
return obj;
}
angular.module('ra.model.services', [])
.run(function($rootScope, raModel) {
// TODO: add configuration option to disable
// convenience method polluting $rootScope
$rootScope.model = function(name, config) {
var model = new raModel(this, name, config);
this[name] = model;
return model;
};
})
.factory('raModel', function($rootScope, $cacheFactory, $location, $log, $q) {
var model_cache = $cacheFactory('raModel');
// Constructor
var raModel = function raModel(scope, name, config) {
var args = Array.prototype.slice.call(arguments),
model;
// Optional first parameter
if (!angular.isObject(args[0]) && args[0] !== false) {
args.unshift($rootScope);
}
scope = args[0];
name = args[1];
config = args[2] || {};
// Extend model with config
this.extend(config);
// Set privileged vars
this.is = config.is || {};
this.opts = config.opts || {};
this.attr_accessible = config.attr_accessible || [];
this.attr_protected = config.attr_protected || [];
this.resource_attribute = config.resource_attribute || 'items';
this.opts.name = this.opts.name || name;
// Privileged accessors
this._scope = function() {
return scope;
};
if (angular.isFunction(this.onload)) {
this.onload.call(this);
}
};
// Public methods
raModel.prototype.init = function(params) {
if (angular.isFunction(this.beforeInit)) {
this.beforeInit.call(this, params);
$log.warn('raModel.beforeInit is deprecated, use init instead');
}
var call = this.get(params);
if (angular.isFunction(this.afterInit)) {
this.afterInit.call(this, call, params);
$log.warn('raModel.init is deprecated, use init instead');
}
if (angular.isFunction(this.config.init)) {
this.config.init.call(this, call, params);
}
this.is.inited = true;
return call;
};
raModel.prototype.get = function(passed_params) {
if (this.opts.cache) {
var cache = this.cached();
if (cache) {
$q.when(cache)
.then(function() {
this.success(cache);
}.bind(this));
return cache;
}
}
var args = [],
call,
params;
if (this.params) {
if (angular.isFunction(this.params)) {
params = this.params();
} else {
params = this.params;
}
}
args.push(
angular.extend({}, params, passed_params),
this._setHeaders.bind(this)
);
if (this.resource) {
var resource = this.resource,
context = this;
if (this.resource_method) {
resource = resource[this.resource_method];
context = this.resource;
}
call = resource.apply(context, args);
if (call && call.$promise) {
this.$promise = call.$promise.then(
this.success.bind(this),
this.error.bind(this)
);
}
} else if (angular.isFunction(this.config.get)) {
call = this.config.get.apply(this, args);
}
this.is.loading = true;
return call;
};
raModel.prototype.update = function() {
this.is.updating = true;
this.is.processing = true;
var updateSuccess = function updateSuccess(response) {
this.is.updating = false;
this.is.processing = false;
this.snapshot();
this._broadcast('updateComplete', 'updateSuccess').data(response);
};
var updateError = function updateError(error) {
this.is.updating = false;
this.is.processing = false;
this._broadcast('updateComplete', 'updateError').data(error);
return $q.reject(error);
};
var promise;
if (angular.isFunction(this.config.update)) {
promise = this.config.update.apply(this, arguments);
}
else if (angular.isFunction(this.$update)) {
promise = this.$update.call(this.getData());
}
else {
throw new Error('Model must have an resource.$update method or update method.');
}
// Make sure we have a promise
promise = $q.when(promise);
if (angular.isFunction(this.config.updateSuccess)) {
promise = promise.then(this.config.updateSuccess.bind(this));
}
if (angular.isFunction(this.config.updateError)) {
promise = promise['catch'](this.config.updateError.bind(this));
}
return promise
.then(
updateSuccess.bind(this),
updateError.bind(this)
);
};
raModel.prototype.success = function(response) {
this._setAttrs(response);
if (this.resource_set !== false) {
if (angular.isArray(response)) {
this[this.resource_attribute] = response;
} else {
extend(this, response);
}
}
this.is.loaded = true;
this.is.loading = false;
if (angular.isFunction(this.config.success)) {
this.config.success.call(this, response, this.$headers);
}
this.snapshot();
if (this.opts.cache &&
this.opts.cache.set !== false) {
this.cache(response);
}
this._broadcast('success', 'complete').data(response, this.$headers);
return response;
};
raModel.prototype.error = function(response) {
this.is.loaded = true;
this.is.loading = false;
if (angular.isFunction(this.config.error)) {
this.config.error.call(this, response);
}
this._broadcast('error', 'complete').data(response);
return $q.reject(response);
};
raModel.prototype.cache = function(data, key) {
// Put in cache object
model_cache.put(this._cacheKey(key), data);
};
raModel.prototype.cached = function(key) {
// Return cache object
return model_cache.get(this._cacheKey(key));
};
raModel.prototype.snapshot = function() {
// Snapshot data
this._original = this.getData();
};
raModel.prototype.reset = function() {
// Reset data
if (angular.isArray(this._original)) {
angular.forEach(this[this.resource_attribute], function(resource) {
for (var i = 0, len = this._original.length; i < len; i++) {
if (angular.isObject(resource) && angular.isObject(this._original[i]) &&
resource.id === this._original[i].id) {
angular.extend(resource, angular.copy(this._original[i]));
break;
}
}
}.bind(this));
} else {
angular.extend(this, angular.copy(this._original));
}
};
raModel.prototype.flush = function(key) {
// Flush cache
model_cache.remove(this._cacheKey(key));
};
raModel.prototype.extend = function(config) {
var self = this;
// Default config
config = config || {};
// Extend config
if (this.config) {
extend(this.config, config);
} else {
this.config = config;
}
// Go through each prop in config (including inherited props/methods) and extend the model with it
for (var prop in config) {
var value = config[prop];
// Extending methods
if (angular.isFunction(value)) {
// Override existing method if first argument name is '$super'
if (argumentNames(value)[0] === '$super') {
var $super = this[prop],
method = angular.copy(value);
// Create a new method and pass original method into the new method
value = function() {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift($super.bind(self));
return method.apply(self, args);
};
this[prop] = value;
}
// Otherwise, only override method if not part of raModel.prototype
else if (angular.isUndefined(raModel.prototype[prop]) ||
angular.isDefined(raModel.prototype[prop]) && raModel.prototype[prop] === Object.prototype[prop]) {
this[prop] = value;
}
}
// Extending properties
else {
this[prop] = value;
}
}
};
var getData = function getData(resource) {
var data;
if (angular.isArray(resource)) {
data = [];
angular.forEach(resource, function(r) {
data.push(getData.call(this, r));
}.bind(this));
} else {
data = {};
angular.forEach(this._getAttrs(), function(attr) {
if (attr in resource) {
data[attr] = angular.copy(resource[attr]);
}
});
}
return data;
};
raModel.prototype.data = raModel.prototype.getData = function(_resource) {
var self = this,
resource;
if (arguments.length > 0) {
resource = _resource;
} else {
resource = this[this.resource_attribute] || this;
}
return getData.call(this, resource);
};
// Privileged methods
raModel.prototype._setHeaders = function(response, headers) {
this.$headers = headers;
this.$headers.parse = function(key) {
return angular.fromJson(this(key));
};
};
// Attribute setter, which makes the attribute available
// for getData()
raModel.prototype.setAttr = function(attr, value) {
this[attr] = value;
this.attr_accessible.push(attr);
if (angular.isArray(this._attrs)) {
this._attrs.push(attr);
}
};
raModel.prototype._setAttrs = function(response) {
var obj;
if (angular.isArray(response) && response.length > 0) {
obj = response[0];
} else {
obj = response;
}
if (angular.isObject(obj)) {
this._attrs = [];
// TODO: might have to refactor this for backwards compatibility
var keys = Object.keys(obj).concat(this.attr_accessible),
self = this;
angular.forEach(keys, function(key) {
if (key) {
var f = key.charAt(0);
if (f !== '$' && f !== '_' && self._attrs.indexOf(key) === -1 && self.attr_protected.indexOf(key) === -1) {
self._attrs.push(key);
}
}
});
}
};
raModel.prototype._getAttrs = function() {
if (angular.isArray(this._attrs) && this._attrs.length > 0) {
return this._attrs;
}
else if (angular.isArray(this.attr_accessible) && this.attr_accessible.length > 0) {
return this.attr_accessible;
}
return [];
};
raModel.prototype._cacheKey = function(key) {
var k = [];
if (key) {
k.push(key);
} else if (this.opts.cache.key !== true) {
k.push(this.opts.cache.key);
} else {
k.push($location.path());
}
k.push(this.opts.name);
return k.join('|');
};
raModel.prototype._broadcast = function() {
var self = this,
scope = self._scope(),
messages = Array.prototype.slice.call(arguments, 0);
var broadcastData = function() {
if (scope && scope.$broadcast) {
var params = Array.prototype.slice.call(arguments, 0);
angular.forEach(messages, function(message) {
var args = angular.copy(params);
args.unshift(self.opts.name +':'+ message);
scope.$broadcast.apply(scope, args);
});
}
};
return { data: broadcastData };
};
return raModel;
});
})();