-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathshared.js
More file actions
64 lines (55 loc) · 1.74 KB
/
shared.js
File metadata and controls
64 lines (55 loc) · 1.74 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
/**
* @mixin SharedModel
*
* @description
*
* Enables caching instances by primary key so a resource with a given id will always refer to the same instance.
*/
'use strict';
angular.module('restmod').factory('SharedModel', ['restmod', function(restmod) {
return restmod.mixin({
$extend : {
Model: {
cache: {},
}
}
}, function() {
this
// this will cache record by its type within cache, as apparently cache
// variable as assigned above will be shared between models
.define('Model.$cache', function(){
var self = this;
if(self.cache.hasOwnProperty(self.identity())) {
return self.cache[self.identity()];
} else {
return self.cache[self.identity()] = {};
}
})
.define('Model.$new', function(_key, _scope) {
var self = this;
if(_key) {
// search for instance with same key, if key is found then return instance
if(self.$cache().hasOwnProperty(_key)) {
var cached = self.$cache()[_key];
if(typeof _scope == 'object'){
cached.$scope = _scope;
}
return cached;
} else {
return (self.$cache()[_key] = this.$super(_key, _scope));
}
} else {
return this.$super(_key, _scope);
}
})
// override $decode to update cache on decoding.
.define('Model.$decode', function(_raw, _mask) {
var self = this,
result = this.$super(_raw, _mask);
if(result.$pk) {
self.$cache()[result.$pk] = this;
}
return this.$super(_raw, _mask);
});
});
}]);