-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvling.js
74 lines (61 loc) · 2.02 KB
/
resolvling.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
(function() {
'use strict';
/*global extend, angular*/
angular.module('resolvling', []).service('Resolvling', ResolvlingFactory);
ResolvlingFactory.$inject = [];
/* @ngInject */
function ResolvlingFactory() {
this.resolvingWith = resolvingWith;
this.insertDataIntoArray = insertDataIntoArray;
this.insertDataIntoObject = insertDataIntoObject;
function Resolvling(value, promise) {
if (!(promise && promise.then))
throw Error("Can't create a resolvling without a promise");
shallowClearAndCopy(value || {}, this);
var updateable = this;
this.$resolved = false;
this.$promise = promise.then(function resolve(data) {
insertDataIntoObject(data, updateable);
return data;
});
}
Resolvling.prototype.toJSON = function() {
var data = angular.extend({}, this);
delete data.$promise;
delete data.$resolved;
return data;
};
function resolvingWith(promise) {
return new Resolvling({}, promise);
}
function insertDataIntoArray(data, array) {
if (!angular.isArray(data) || !angular.isArray(array))
throw new Error('Please provide an array as value and data.');
data.forEach(function(item) {
array.push(item);
});
}
function insertDataIntoObject(data, value) {
if (angular.isArray(data) || angular.isArray(value))
throw new Error(
'Please provide an object as value and data. ' +
'Use Updateable.insertDataIntoArray(data, array) if you wish to update an array.')
var promise = value.$promise;
shallowClearAndCopy(data, value);
value.$promise = promise;
value.$resolved = true;
}
function shallowClearAndCopy(src, dst) {
angular.forEach(dst, function(value, key) {
delete dst[key];
});
for (var key in src) {
if (src.hasOwnProperty(key) &&
!(key.charAt(0) === '$' && key.charAt(1) === '$')) {
dst[key] = src[key];
}
}
return dst;
}
}
})();