CompositeWire.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.CompositeWire"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.wire.CompositeWire"] = true;
  8. dojo.provide("dojox.wire.CompositeWire");
  9. dojo.require("dojox.wire._base");
  10. dojo.require("dojox.wire.Wire");
  11. dojo.declare("dojox.wire.CompositeWire", dojox.wire.Wire, {
  12. // summary:
  13. // A Wire for composite values in object or array
  14. // description:
  15. // This class has multiple child Wires for object properties or array
  16. // elements.
  17. // When an object with Wires is specified to 'children' property, they
  18. // are used to get or set an object with property values.
  19. // When an array of Wiares is specified to 'children' property, they
  20. // are used to get or set an array with element values.
  21. _wireClass: "dojox.wire.CompositeWire",
  22. constructor: function(/*Object*/args){
  23. // summary:
  24. // Initialize properties
  25. // description:
  26. // If object properties or array elements specified in 'children'
  27. // property are not Wires, Wires are created from them as
  28. // arguments, with 'parent' property set to this Wire instance.
  29. // args:
  30. // Arguments to initialize properties
  31. // children:
  32. // An object or array containing child Wires
  33. this._initializeChildren(this.children);
  34. },
  35. _getValue: function(/*Object||Array*/object){
  36. // summary:
  37. // Return an object with property values or an array with element
  38. // values
  39. // description:
  40. // This method calls getValue() method of the child Wires with
  41. // 'object' argument and returns an object with the values as
  42. // properties or an arary of the values as elements.
  43. // object:
  44. // A root object
  45. // returns:
  46. // An object or array with values
  47. if(!object || !this.children){
  48. return object; //Object||Array
  49. }
  50. var value = (dojo.isArray(this.children) ? [] : {}); // array or object
  51. for(var c in this.children){
  52. value[c] = this.children[c].getValue(object);
  53. }
  54. return value;//Object||Array
  55. },
  56. _setValue: function(/*Object||Array*/object, /*Object||Array*/value){
  57. // summary:
  58. // Set an object properties or an array elements to an object
  59. // desription:
  60. // This method calls setValues() method of the child Wires with
  61. // a corresponding property or element in 'value' argument and
  62. // 'object' argument.
  63. // object:
  64. // A root object
  65. // value:
  66. // An object or array with values to set
  67. // returns:
  68. // 'object'
  69. if(!object || !this.children){
  70. return object; //Object||Array
  71. }
  72. for(var c in this.children){
  73. this.children[c].setValue(value[c], object);
  74. }
  75. return object; //Object||Array
  76. },
  77. _initializeChildren: function(/*Object||Array*/children){
  78. // summary:
  79. // Initialize child Wires
  80. // description:
  81. // If object properties or array elements specified in 'children'
  82. // argument are not Wires, Wires are created from them as
  83. // arguments, with 'parent' property set to this Wire instance.
  84. // children:
  85. // An object or array containing child Wires
  86. if(!children){
  87. return; //undefined
  88. }
  89. for(var c in children){
  90. var child = children[c];
  91. child.parent = this;
  92. if(!dojox.wire.isWire(child)){
  93. children[c] = dojox.wire.create(child);
  94. }
  95. }
  96. }
  97. });
  98. }