forked from stemey/dojo-generate-form
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdateStateful.js
86 lines (75 loc) · 2.63 KB
/
updateStateful.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
75
76
77
78
79
80
81
82
83
84
85
86
define([
"dojo/_base/array",
"dojo/_base/lang",
"dojo/Stateful",
"dojox/mvc/StatefulArray",
"./getStateful"
], function(array, lang, Stateful, StatefulArray, getStateful){
var updateStatefulOptions = {
// summary:
// Options used for dojox/mvc/getStateful().
getType: function(/*Anything*/ v){
// summary:
// Returns the type of the given value.
// v: Anything
// The value.
return lang.isArray(v) ? "array" : v != null && {}.toString.call(v) == "[object Object]" ? "object" : "value";
},
updateStatefulArray: function(/*Anything[]*/ a,modelHandle){
// summary:
// Returns the stateful version of the given array.
// a: Anything[]
// The array.
this.resetMeta(modelHandle,a);
if (modelHandle.value) {
modelHandle.value.splice(0,modelHandle.value.length);
}else{
modelHandle.value=new StatefulArray([]);
}
array.forEach(a, function(item){ modelHandle.value.push(getStateful(item)); }, this); // dojox/mvc/StatefulArray
},
updateStatefulObject: function(/*Object*/ o, modelHandle){
// summary:
// Returns the stateful version of the given object.
// o: Object
// The object.
var target = modelHandle.value;
this.resetMeta(modelHandle,o);
for(var s in o){
updateStateful(o[s],target.get(s), this);
}
for(var s in target._attrPairNames){
console.log("---"+s+" "+typeof s);
if (typeof o[s]=="undefined") {
delete target[s];
}
}
},
resetMeta: function(modelHandle,plainValue) {
modelHandle.set("oldValue",plainValue);
modelHandle.set("valid",true);
},
updateStatefulValue: function(/*Anything*/ v, modelHandle){
// summary:
// Just returns the given value.
this.resetMeta(modelHandle,v);
modelHandle.set("value",v);
}
};
var updateStateful = function(/*Anything*/ value, modelHandle,/*dojox/mvc/getStatefulOptions*/ options){
// summary:
// Create a dojo/Stateful object from a raw value.
// description:
// Recursively iterates the raw value given, and convert them to stateful ones.
// value: Anything
// The raw value.
// options: dojox/mvc/getStatefulOptions
// The object that defines how model object should be created from plain object hierarchy.
// returns: Anything
// The converted value.
return (options || updateStatefulOptions)["updateStateful" + (options || updateStatefulOptions).getType(value).replace(/^[a-z]/, function(c){ return c.toUpperCase(); })](value,modelHandle); // Anything
};
// lang.setObject() thing is for back-compat, remove it in 2.0
//return lang.setObject("dojox.mvc.getStateful", lang.mixin(getStateful, getStatefulOptions));
return updateStateful;
});