DataWire.js 5.6 KB

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