-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathknockout.pauseable.js
More file actions
153 lines (136 loc) · 4.3 KB
/
knockout.pauseable.js
File metadata and controls
153 lines (136 loc) · 4.3 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
/* A fleshed-out implementation of the pausable observables
* for both array and non-array versions, based on the the work
* done by Ryan Niemeyer.
* (http://www.knockmeout.net/2011/04/pausing-notifications-in-knockoutjs.html)
*/
ko.extenders.pauseable = function(target) {
var _actual = target;
var _cachedValue = null;
var _isPaused = ko.observable(false);
var result = null;
result = new ko.computed({
read: function() {
if(!_isPaused())
return _actual();
else
return _cachedValue;
},
write: function(newValue) {
if(!_isPaused())
_actual(newValue);
else
_cachedValue = newValue;
}
});
result.pause = function() {
_cachedValue = _actual();
_isPaused(true);
}
result.resume = function() {
_actual(_cachedValue);
_cachedValue = null;
_isPaused(false);
}
// Minification in KO breaks overriding "valueWillMutate" and "valueHasMutated",
// as they aren't referenced by those names once minified.
// So we have to target notifySubscribers instead...
var _notifySubscribers = _actual.notifySubscribers;
_actual.notifySubscribers = function() {
if(!_isPaused()) {
_notifySubscribers.apply(target, arguments);
}
}
// Then, find the underlying minimized functions and apply them to the computed observable
// First, find "valueWillMutate"
for(var fn in _actual) {
if(_actual[fn] === _actual.valueWillMutate) {
result.valueWillMutate = _actual.valueWillMutate;
result[fn] = result.valueWillMutate;
break;
}
}
// Then, find "valueHasMutated"
for(var fn in _actual) {
if(_actual[fn] === _actual.valueHasMutated) {
result.valueHasMutated = _actual.valueHasMutated;
result[fn] = result.valueHasMutated;
break;
}
}
if(target.removeAll)
{
result.push = function(value) {
if(!_isPaused())
return(_actual.push(value));
else
return(_cachedValue.push(value));
}
result.remove = function(valueOrPredicate) {
if(!_isPaused())
return(_actual.remove(valueOrPredicate));
else
{
//Modified code from the KO source
var underlyingArray = _cachedValue;
var removedValues = [];
var predicate = typeof valueOrPredicate == "function" ? valueOrPredicate : function (value) { return value === valueOrPredicate; };
for (var i = 0; i < underlyingArray.length; i++) {
var value = underlyingArray[i];
if (predicate(value)) {
removedValues.push(value);
underlyingArray.splice(i, 1);
i--;
}
}
return removedValues;
}
}
result.removeAll = function(arrayOfValues) {
if(!_isPaused())
return(_actual.removeAll(arrayOfValues));
else
{
// If you passed zero args, we remove everything
if (arrayOfValues === undefined) {
var underlyingArray = _cachedValue;
var allValues = underlyingArray.slice(0);
underlyingArray.splice(0, underlyingArray.length);
return allValues;
}
// If you passed an arg, we interpret it as an array of entries to remove
if (!arrayOfValues)
return [];
return this['remove'](function (value) {
return ko.utils.arrayIndexOf(arrayOfValues, value) >= 0;
});
}
}
// Populate ko.observableArray.fn with read/write functions from native arrays
ko.utils.arrayForEach(["pop", "push", "reverse", "shift", "sort", "splice", "unshift"], function (methodName) {
result[methodName] = function () {
if(!_isPaused())
{
var methodCallResult = _actual[methodName].apply(_actual, arguments);
}
else
{
var underlyingArray = _cachedValue;
var methodCallResult = underlyingArray[methodName].apply(underlyingArray, arguments);
return methodCallResult;
}
};
});
// Populate ko.observableArray.fn with read-only functions from native arrays
ko.utils.arrayForEach(["slice"], function (methodName) {
result[methodName] = function () {
var underlyingArray = null;
if(!_isPaused())
underlyingArray = _actual;
else
underlyingArray = _cachedValue;
return underlyingArray[methodName].apply(underlyingArray, arguments);
};
});
}
return result;
}