1 | define([ |
---|
2 | "dojo/_base/array", |
---|
3 | "dojo/_base/lang", |
---|
4 | "dojo/Stateful", |
---|
5 | "./StatefulArray" |
---|
6 | ], function(array, lang, Stateful, StatefulArray){ |
---|
7 | var getStatefulOptions = { |
---|
8 | // summary: |
---|
9 | // Options used for dojox/mvc/getStateful(). |
---|
10 | |
---|
11 | getType: function(/*Anything*/ v){ |
---|
12 | // summary: |
---|
13 | // Returns the type of the given value. |
---|
14 | // v: Anything |
---|
15 | // The value. |
---|
16 | |
---|
17 | return lang.isArray(v) ? "array" : v != null && {}.toString.call(v) == "[object Object]" ? "object" : "value"; |
---|
18 | }, |
---|
19 | |
---|
20 | getStatefulArray: function(/*Anything[]*/ a){ |
---|
21 | // summary: |
---|
22 | // Returns the stateful version of the given array. |
---|
23 | // a: Anything[] |
---|
24 | // The array. |
---|
25 | |
---|
26 | return new StatefulArray(array.map(a, function(item){ return getStateful(item, this); }, this)); // dojox/mvc/StatefulArray |
---|
27 | }, |
---|
28 | |
---|
29 | getStatefulObject: function(/*Object*/ o){ |
---|
30 | // summary: |
---|
31 | // Returns the stateful version of the given object. |
---|
32 | // o: Object |
---|
33 | // The object. |
---|
34 | |
---|
35 | var stateful = new Stateful(); |
---|
36 | for(var s in o){ |
---|
37 | stateful[s] = getStateful(o[s], this); |
---|
38 | } |
---|
39 | return stateful; // dojo/Stateful |
---|
40 | }, |
---|
41 | |
---|
42 | getStatefulValue: function(/*Anything*/ v){ |
---|
43 | // summary: |
---|
44 | // Just returns the given value. |
---|
45 | |
---|
46 | return v; // Anything |
---|
47 | } |
---|
48 | }; |
---|
49 | |
---|
50 | var getStateful = function(/*Anything*/ value, /*dojox/mvc/getStatefulOptions*/ options){ |
---|
51 | // summary: |
---|
52 | // Create a dojo/Stateful object from a raw value. |
---|
53 | // description: |
---|
54 | // Recursively iterates the raw value given, and convert them to stateful ones. |
---|
55 | // value: Anything |
---|
56 | // The raw value. |
---|
57 | // options: dojox/mvc/getStatefulOptions |
---|
58 | // The object that defines how model object should be created from plain object hierarchy. |
---|
59 | // returns: Anything |
---|
60 | // The converted value. |
---|
61 | |
---|
62 | return (options || getStateful)["getStateful" + (options || getStateful).getType(value).replace(/^[a-z]/, function(c){ return c.toUpperCase(); })](value); // Anything |
---|
63 | }; |
---|
64 | |
---|
65 | // lang.setObject() thing is for back-compat, remove it in 2.0 |
---|
66 | return lang.setObject("dojox.mvc.getStateful", lang.mixin(getStateful, getStatefulOptions)); |
---|
67 | }); |
---|