source: Dev/trunk/src/client/dojox/wire/DataWire.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 5.5 KB
Line 
1dojo.provide("dojox.wire.DataWire");
2
3dojo.require("dojox.wire.Wire");
4
5dojo.declare("dojox.wire.DataWire", dojox.wire.Wire, {
6        // summary:
7        //              A Wire for item attributes of data stores
8        // description:
9        //              This class accesses item attributes of data stores with a dotted
10        //              notation of attribute names specified to 'attribute' property,
11        //              using data APIs of a data store specified to 'dataStore' property.
12        //              The root object for this class must be an item of the data store.
13        //              Intermediate attribute names in the dotted notation specify
14        //              attributes for child items, which are used for repeated calls to
15        //              data APIs until reached to a descendant attribute.
16        //              Attribute names may have an array index, such as "a[0]", to
17        //              identify an array element of the attribute value.
18       
19        _wireClass: "dojox.wire.DataWire",
20
21        constructor: function(/*Object*/ args){
22                // summary:
23                //              Initialize properties
24                // description:
25                //              If 'dataStore' property is not specified, but 'parent' property
26                //              is specified, 'dataStore' property is copied from the parent.
27                // args:
28                //              Arguments to initialize properties:
29                //
30                //              - dataStore: A data store
31                //              - attribute: A dotted notation to a descendant attribute
32                if(!this.dataStore && this.parent){
33                        this.dataStore = this.parent.dataStore;
34                }
35        },
36        _getValue: function(/*Object*/object){
37                // summary:
38                //              Return an attribute value of an item
39                // description:
40                //              This method uses a root item passed in 'object' argument and
41                //              'attribute' property to call getValue() method of
42                //              'dataStore'.
43                //              If an attribute name have an array suffix ("[]"), getValues()
44                //              method is called, instead.
45                //              If an index is specified in the array suffix, an array element
46                //              for the index is returned, instead of the array itself.
47                // object:
48                //              A root item
49                // returns:
50                //              A value found, otherwise 'undefined'
51                if(!object || !this.attribute || !this.dataStore){
52                        return object; //Object
53                }
54
55                var value = object;
56                var list = this.attribute.split('.');
57                for(var i in list){
58                        value = this._getAttributeValue(value, list[i]);
59                        if(!value){
60                                return undefined; //undefined
61                        }
62                }
63                return value; //anything
64        },
65
66        _setValue: function(/*Object*/object, /*anything*/value){
67                // summary:
68                //              Set an attribute value to an item
69                // description:
70                //              This method uses a root item passed in 'object' argument and
71                //              'attribute' property to identify an item.
72                //              Then, setValue() method of 'dataStore' is called with a leaf
73                //              attribute name and 'value' argument.
74                //              If an attribute name have an array suffix ("[]"), setValues()
75                //              method is called, instead.
76                //              If an index is specified in the array suffix, an array element
77                //              for the index is set to 'value', instead of the array itself.
78                // object:
79                //              A root item
80                // value:
81                //              A value to set
82                // returns:
83                //              'object', or 'undefined' for invalid attribute
84                if(!object || !this.attribute || !this.dataStore){
85                        return object; //Object
86                }
87
88                var item = object;
89                var list = this.attribute.split('.');
90                var last = list.length - 1;
91                for(var i = 0; i < last; i++){
92                        item = this._getAttributeValue(item, list[i]);
93                        if(!item){
94                                return undefined; //undefined
95                        }
96                }
97                this._setAttributeValue(item, list[last], value);
98                return object; //Object
99        },
100
101        _getAttributeValue: function(/*Object*/item, /*String*/attribute){
102                // summary:
103                //              Return an attribute value of an item
104                // description:
105                //              This method uses an item passed in 'item' argument and
106                //              'attribute' argument to call getValue() method of 'dataStore'.
107                //              If an attribute name have an array suffix ("[]"), getValues()
108                //              method is called, instead.
109                //              If an index is specified in the array suffix, an array element
110                //              for the index is returned, instead of the array itself.
111                // item:
112                //              An item
113                // attribute:
114                //              An attribute name
115                // returns:
116                //              A value found, otherwise 'undefined'
117                var value = undefined;
118                var i1 = attribute.indexOf('[');
119                if(i1 >= 0){
120                        var i2 = attribute.indexOf(']');
121                        var index = attribute.substring(i1 + 1, i2);
122                        attribute = attribute.substring(0, i1);
123                        var array = this.dataStore.getValues(item, attribute);
124                        if(array){
125                                if(!index){ // return array for "attribute[]"
126                                        value = array;
127                                }else{
128                                        value = array[index];
129                                }
130                        }
131                }else{
132                        value = this.dataStore.getValue(item, attribute);
133                }
134                return value; //anything
135        },
136
137        _setAttributeValue: function(/*Object*/item, /*String*/attribute, /*anything*/value){
138                // summary:
139                //              Set an attribute value to an item
140                // description:
141                //              This method uses an item passed in 'item' argument and
142                //              'attribute' argument to call setValue() method of 'dataStore'
143                //              with 'value' argument.
144                //              If an attribute name have an array suffix ("[]"), setValues()
145                //              method is called, instead.
146                //              If an index is specified in the array suffix, an array element
147                //              for the index is set to 'value', instead of the array itself.
148                // item:
149                //              An item
150                // attribute:
151                //              An attribute name
152                // value:
153                //              A value to set
154                var i1 = attribute.indexOf('[');
155                if(i1 >= 0){
156                        var i2 = attribute.indexOf(']');
157                        var index = attribute.substring(i1 + 1, i2);
158                        attribute = attribute.substring(0, i1);
159                        var array = null;
160                        if(!index){ // replace whole array for "attribute[]"
161                                array = value;
162                        }else{
163                                array = this.dataStore.getValues(item, attribute);
164                                if(!array){
165                                        array = [];
166                                }
167                                array[index] = value;
168                        }
169                        this.dataStore.setValues(item, attribute, array);
170                }else{
171                        this.dataStore.setValue(item, attribute, value);
172                }
173        }
174});
Note: See TracBrowser for help on using the repository browser.