-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathknockout.wrap.js
More file actions
209 lines (170 loc) · 4.27 KB
/
knockout.wrap.js
File metadata and controls
209 lines (170 loc) · 4.27 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
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
// Knockout Fast Mapping v0.1
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
(function (factory) {
// Module systems magic dance.
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node: hard-coded dependency on "knockout"
factory(require("knockout"), exports);
} else if (typeof define === "function" && define["amd"]) {
// AMD anonymous module with hard-coded dependency on "knockout"
define(["knockout", "exports"], factory);
} else {
// <script> tag: use the global `ko` object, attaching a `wrap` property
factory(ko, ko.wrap = {});
}
}(function (ko, exports) {
// this function mimics ko.mapping
exports.fromJS = function(jsObject, computedFunctions)
{
reset();
return wrap(jsObject, computedFunctions);
}
// this function unwraps the outer for assigning the result to an observable
// see https://github.com/SteveSanderson/knockout/issues/517
exports.updateFromJS = function(observable, jsObject, computedFunctions)
{
reset();
return observable(ko.utils.unwrapObservable(wrap(jsObject, computedFunctions)));
}
exports.fromJSON = function (jsonString, computedFunctions) {
var parsed = ko.utils.parseJson(jsonString);
arguments[0] = parsed;
return exports.fromJS.apply(this, computedFunctions);
};
exports.toJS = function (observable) {
return unwrap(observable);
}
exports.toJSON = function (observable) {
var plainJavaScriptObject = exports.toJS(observable);
return ko.utils.stringifyJson(plainJavaScriptObject);
};
function typeOf(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (value.constructor == Date)
s = 'date';
else if (Object.prototype.toString.call(value) == '[object Array]')
s = 'array';
} else {
s = 'null';
}
}
return s;
}
// unwrapping
function unwrapObject(o)
{
var t = {};
for (var k in o)
{
var v = o[k];
if (ko.isComputed(v))
continue;
t[k] = unwrap(v);
}
return t;
}
function unwrapArray(a)
{
var r = [];
if (!a || a.length == 0)
return r;
for (var i = 0, l = a.length; i < l; ++i)
r.push(unwrap(a[i]));
return r;
}
function unwrap(v)
{
var isObservable = ko.isObservable(v);
if (isObservable)
{
var val = v();
return unwrap(val);
}
else
{
if (typeOf(v) == "array")
{
return unwrapArray(v);
}
else if (typeOf(v) == "object")
{
return unwrapObject(v);
}
else
{
return v;
}
}
}
function reset()
{
parents = [{obj: null, wrapped: null, lvl: ""}];
}
// wrapping
function wrapObject(o, computedFunctions)
{
// check for infinite recursion
for (var i = 0; i < parents.length; ++i) {
if (parents[i].obj === o) {
return parents[i].wrapped;
}
}
var t = {};
for (var k in o)
{
var v = o[k];
parents.push({obj: o, wrapped: t, lvl: currentLvl() + "/" + k});
t[k] = wrap(v, computedFunctions);
parents.pop();
}
if (computedFunctions && computedFunctions[currentLvl()])
t = computedFunctions[currentLvl()](t);
if (hasES5Plugin())
ko.track(t);
return t;
}
function wrapArray(a, computedFunctions)
{
var r = ko.observableArray();
if (!a || a.length == 0)
return r;
for (var i = 0, l = a.length; i < l; ++i)
r.push(wrap(a[i], computedFunctions));
return r;
}
// a stack, used for two purposes:
// - circular reference checking
// - computed functions
var parents;
function currentLvl()
{
return parents[parents.length-1].lvl;
}
function wrap(v, computedFunctions)
{
if (typeOf(v) == "array")
{
return wrapArray(v, computedFunctions);
}
else if (typeOf(v) == "object")
{
return wrapObject(v, computedFunctions);
}
else
{
if (!hasES5Plugin() && typeof v !== 'function')
{
var t = ko.observable();
t(v);
return t;
} else
return v;
}
}
function hasES5Plugin()
{
return ko.track != null;
}
}));