Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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