DataWire.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.wire.DataWire"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.wire.DataWire"] = true;
  8. dojo.provide("dojox.wire.DataWire");
  9. dojo.require("dojox.wire.Wire");
  10. dojo.declare("dojox.wire.DataWire", dojox.wire.Wire, {
  11. // summary:
  12. // A Wire for item attributes of data stores
  13. // description:
  14. // This class accesses item attributes of data stores with a dotted
  15. // notation of attribute names specified to 'attribute' property,
  16. // using data APIs of a data store specified to 'dataStore' property.
  17. // The root object for this class must be an item of the data store.
  18. // Intermediate attribute names in the dotted notation specify
  19. // attributes for child items, which are used for repeated calls to
  20. // data APIs until reached to a descendant attribute.
  21. // Attribute names may have an array index, such as "a[0]", to
  22. // identify an array element of the attribute value.
  23. _wireClass: "dojox.wire.DataWire",
  24. constructor: function(/*Object*/args){
  25. // summary:
  26. // Initialize properties
  27. // description:
  28. // If 'dataStore' property is not specified, but 'parent' property
  29. // is specified, 'dataStore' property is copied from the parent.
  30. // args:
  31. // Arguments to initialize properties
  32. // dataStore:
  33. // A data store
  34. // attribute:
  35. // A dotted notation to a descendant attribute
  36. if(!this.dataStore && this.parent){
  37. this.dataStore = this.parent.dataStore;
  38. }
  39. },
  40. _getValue: function(/*Object*/object){
  41. // summary:
  42. // Return an attribute value of an item
  43. // description:
  44. // This method uses a root item passed in 'object' argument and
  45. // 'attribute' property to call getValue() method of
  46. // 'dataStore'.
  47. // If an attribute name have an array suffix ("[]"), getValues()
  48. // method is called, instead.
  49. // If an index is specified in the array suffix, an array element
  50. // for the index is returned, instead of the array itself.
  51. // object:
  52. // A root item
  53. // returns:
  54. // A value found, otherwise 'undefined'
  55. if(!object || !this.attribute || !this.dataStore){
  56. return object; //Object
  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. _setValue: function(/*Object*/object, /*anything*/value){
  69. // summary:
  70. // Set an attribute value to an item
  71. // description:
  72. // This method uses a root item passed in 'object' argument and
  73. // 'attribute' property to identify an item.
  74. // Then, setValue() method of 'dataStore' is called with a leaf
  75. // attribute name and 'value' argument.
  76. // If an attribute name have an array suffix ("[]"), setValues()
  77. // method is called, instead.
  78. // If an index is specified in the array suffix, an array element
  79. // for the index is set to 'value', instead of the array itself.
  80. // object:
  81. // A root item
  82. // value:
  83. // A value to set
  84. // returns:
  85. // 'object', or 'undefined' for invalid attribute
  86. if(!object || !this.attribute || !this.dataStore){
  87. return object; //Object
  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. _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. _setAttributeValue: function(/*Object*/item, /*String*/attribute, /*anything*/value){
  137. // summary:
  138. // Set an attribute value to an item
  139. // description:
  140. // This method uses an item passed in 'item' argument and
  141. // 'attribute' argument to call setValue() method of 'dataStore'
  142. // with 'value' argument.
  143. // If an attribute name have an array suffix ("[]"), setValues()
  144. // method is called, instead.
  145. // If an index is specified in the array suffix, an array element
  146. // for the index is set to 'value', instead of the array itself.
  147. // item:
  148. // An item
  149. // attribute:
  150. // An attribute name
  151. // value:
  152. // A value to set
  153. var i1 = attribute.indexOf('[');
  154. if(i1 >= 0){
  155. var i2 = attribute.indexOf(']');
  156. var index = attribute.substring(i1 + 1, i2);
  157. attribute = attribute.substring(0, i1);
  158. var array = null;
  159. if(!index){ // replace whole array for "attribute[]"
  160. array = value;
  161. }else{
  162. array = this.dataStore.getValues(item, attribute);
  163. if(!array){
  164. array = [];
  165. }
  166. array[index] = value;
  167. }
  168. this.dataStore.setValues(item, attribute, array);
  169. }else{
  170. this.dataStore.setValue(item, attribute, value);
  171. }
  172. }
  173. });
  174. }