DataWire.js 6.4 KB

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