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