source: Dev/branches/rest-dojo-ui/client/dojox/wire/DataWire.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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                //              dataStore:
30                //                      A data store
31                //              attribute:
32                //                      A dotted notation to a descendant attribute
33                if(!this.dataStore && this.parent){
34                        this.dataStore = this.parent.dataStore;
35                }
36        },
37        _getValue: function(/*Object*/object){
38                //      summary:
39                //              Return an attribute value of an item
40                //      description:
41                //              This method uses a root item passed in 'object' argument and
42                //              'attribute' property to call getValue() method of
43                //              'dataStore'.
44                //              If an attribute name have an array suffix ("[]"), getValues()
45                //              method is called, instead.
46                //              If an index is specified in the array suffix, an array element
47                //              for the index is returned, instead of the array itself.
48                //      object:
49                //              A root item
50                //      returns:
51                //              A value found, otherwise 'undefined'
52                if(!object || !this.attribute || !this.dataStore){
53                        return object; //Object
54                }
55
56                var value = object;
57                var list = this.attribute.split('.');
58                for(var i in list){
59                        value = this._getAttributeValue(value, list[i]);
60                        if(!value){
61                                return undefined; //undefined
62                        }
63                }
64                return value; //anything
65        },
66
67        _setValue: function(/*Object*/object, /*anything*/value){
68                //      summary:
69                //              Set an attribute value to an item
70                //      description:
71                //              This method uses a root item passed in 'object' argument and
72                //              'attribute' property to identify an item.
73                //              Then, setValue() method of 'dataStore' is called with a leaf
74                //              attribute name and 'value' argument.
75                //              If an attribute name have an array suffix ("[]"), setValues()
76                //              method is called, instead.
77                //              If an index is specified in the array suffix, an array element
78                //              for the index is set to 'value', instead of the array itself.
79                //      object:
80                //              A root item
81                //      value:
82                //              A value to set
83                //      returns:
84                //              'object', or 'undefined' for invalid attribute
85                if(!object || !this.attribute || !this.dataStore){
86                        return object; //Object
87                }
88
89                var item = object;
90                var list = this.attribute.split('.');
91                var last = list.length - 1;
92                for(var i = 0; i < last; i++){
93                        item = this._getAttributeValue(item, list[i]);
94                        if(!item){
95                                return undefined; //undefined
96                        }
97                }
98                this._setAttributeValue(item, list[last], value);
99                return object; //Object
100        },
101
102        _getAttributeValue: function(/*Object*/item, /*String*/attribute){
103                //      summary:
104                //              Return an attribute value of an item
105                //      description:
106                //              This method uses an item passed in 'item' argument and
107                //              'attribute' argument to call getValue() method of 'dataStore'.
108                //              If an attribute name have an array suffix ("[]"), getValues()
109                //              method is called, instead.
110                //              If an index is specified in the array suffix, an array element
111                //              for the index is returned, instead of the array itself.
112                //      item:
113                //              An item
114                //      attribute
115                //              An attribute name
116                //      returns:
117                //              A value found, otherwise 'undefined'
118                var value = undefined;
119                var i1 = attribute.indexOf('[');
120                if(i1 >= 0){
121                        var i2 = attribute.indexOf(']');
122                        var index = attribute.substring(i1 + 1, i2);
123                        attribute = attribute.substring(0, i1);
124                        var array = this.dataStore.getValues(item, attribute);
125                        if(array){
126                                if(!index){ // return array for "attribute[]"
127                                        value = array;
128                                }else{
129                                        value = array[index];
130                                }
131                        }
132                }else{
133                        value = this.dataStore.getValue(item, attribute);
134                }
135                return value; //anything
136        },
137
138        _setAttributeValue: function(/*Object*/item, /*String*/attribute, /*anything*/value){
139                //      summary:
140                //              Set an attribute value to an item
141                //      description:
142                //              This method uses an item passed in 'item' argument and
143                //              'attribute' argument to call setValue() method of 'dataStore'
144                //              with 'value' argument.
145                //              If an attribute name have an array suffix ("[]"), setValues()
146                //              method is called, instead.
147                //              If an index is specified in the array suffix, an array element
148                //              for the index is set to 'value', instead of the array itself.
149                //      item:
150                //              An item
151                //      attribute:
152                //              An attribute name
153                //      value:
154                //              A value to set
155                var i1 = attribute.indexOf('[');
156                if(i1 >= 0){
157                        var i2 = attribute.indexOf(']');
158                        var index = attribute.substring(i1 + 1, i2);
159                        attribute = attribute.substring(0, i1);
160                        var array = null;
161                        if(!index){ // replace whole array for "attribute[]"
162                                array = value;
163                        }else{
164                                array = this.dataStore.getValues(item, attribute);
165                                if(!array){
166                                        array = [];
167                                }
168                                array[index] = value;
169                        }
170                        this.dataStore.setValues(item, attribute, array);
171                }else{
172                        this.dataStore.setValue(item, attribute, value);
173                }
174        }
175});
Note: See TracBrowser for help on using the repository browser.