-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-computed.js
68 lines (58 loc) · 2.18 KB
/
angular-computed.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
(function() {'use strict';
angular.module('cloudless.computed', [])
.service('$computed', ['$rootScope', '$q', function($rootScope, $q) {
function touchKey(name) {
return '$$computed-touch-' + name;
}
function controller(context, name) {
return {
touch: function() {
return (context[touchKey(name)] = (context[touchKey(name)] || 0) + 1);
}
};
}
return function angularComputed(context, name, properties) {
/* computed(this) in controller injects service into the controller context */
if (typeof context === 'object' && name === undefined) {
context.$computed = angularComputed;
return context;
}
/* computed(name, properties) uses this for context */
if (typeof context === 'string') {
properties = name;
name = context;
context = this;
}
/* computed([context], name) returns controller */
if (properties === undefined) {
return controller(context, name);
}
/* last element in properties array is a compute function */
var valueFunc = properties.splice(properties.length - 1)[0],
version = 0;
/* watch a group of specified properties and evaluate compute function */
$rootScope.$watchGroup(properties.concat([touchKey(name)]).map(function(item) {
if (typeof item === 'string') {
return function() { return context[item]; };
} else if (typeof item === 'function') {
return angular.bind(context, item);
}
}), function(newValues) {
var thisVer = ++version,
valid = function valid() { return thisVer === version; },
valueArgs = newValues.slice(0, newValues.length - 1).concat([valid]),
promise = $q.when(valueFunc.apply(context, valueArgs));
promise.then(function(response) {
if (valid()) {
context[name] = response;
}
});
});
return controller(context, name);
};
}])
/* provide scope.$computed(name, properties) on all scopes */
.run(['$rootScope', '$computed', function($rootScope, $computed) {
$computed($rootScope);
}]);
})();