Data.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.ml.Data"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.wire.ml.Data"] = true;
  8. dojo.provide("dojox.wire.ml.Data");
  9. dojo.provide("dojox.wire.ml.DataProperty");
  10. dojo.require("dijit._Widget");
  11. dojo.require("dijit._Container");
  12. dojo.require("dojox.wire.ml.util");
  13. dojo.declare("dojox.wire.ml.Data", [dijit._Widget, dijit._Container], {
  14. // summary:
  15. // A widget for a data object
  16. // description:
  17. // This widget represents an object with '_properties' property.
  18. // If child 'DataProperty' widgets exist, they are used to initialize
  19. // propertiy values of '_properties' object.
  20. startup: function(){
  21. // summary:
  22. // Call _initializeProperties()
  23. // description:
  24. // See _initializeProperties().
  25. this._initializeProperties();
  26. },
  27. _initializeProperties: function(/*Boolean*/reset){
  28. // summary:
  29. // Initialize a data object
  30. // description:
  31. // If this widget has child DataProperty widgets, their getValue()
  32. // methods are called and set the return value to a property
  33. // specified by 'name' attribute of the child widgets.
  34. // reset:
  35. // A boolean to reset current properties
  36. if(!this._properties || reset){
  37. this._properties = {};
  38. }
  39. var children = this.getChildren();
  40. for(var i in children){
  41. var child = children[i];
  42. if((child instanceof dojox.wire.ml.DataProperty) && child.name){
  43. this.setPropertyValue(child.name, child.getValue());
  44. }
  45. }
  46. },
  47. getPropertyValue: function(/*String*/property){
  48. // summary:
  49. // Return a property value
  50. // description:
  51. // This method returns the value of a property, specified with
  52. // 'property' argument, in '_properties' object.
  53. // property:
  54. // A property name
  55. // returns:
  56. // A property value
  57. return this._properties[property]; //anything
  58. },
  59. setPropertyValue: function(/*String*/property, /*anything*/value){
  60. // summary:
  61. // Store a property value
  62. // description:
  63. // This method stores 'value' as a property, specified with
  64. // 'property' argument, in '_properties' object.
  65. // property:
  66. // A property name
  67. // value:
  68. // A property value
  69. this._properties[property] = value;
  70. }
  71. });
  72. dojo.declare("dojox.wire.ml.DataProperty", [dijit._Widget, dijit._Container], {
  73. // summary:
  74. // A widget to define a data property
  75. // description:
  76. // Attributes of this widget are used to add a property to the parent
  77. // Data widget.
  78. // 'type' attribute specifies one of "string", "number", "boolean",
  79. // "array", "object" and "element" (DOM Element)
  80. // (default to "string").
  81. // If 'type' is "array" or "object", child DataProperty widgets are
  82. // used to initialize the array elements or the object properties.
  83. // name:
  84. // A property name
  85. // type:
  86. // A property type name
  87. // value:
  88. // A property value
  89. name: "",
  90. type: "",
  91. value: "",
  92. _getValueAttr: function(){
  93. return this.getValue();
  94. },
  95. getValue: function(){
  96. // summary:
  97. // Returns a property value
  98. // description:
  99. // If 'type' is specified, 'value' attribute is converted to
  100. // the specified type and returned.
  101. // Otherwise, 'value' attribute is returned as is.
  102. // returns:
  103. // A property value
  104. var value = this.value;
  105. if(this.type){
  106. if(this.type == "number"){
  107. value = parseInt(value);
  108. }else if(this.type == "boolean"){
  109. value = (value == "true");
  110. }else if(this.type == "array"){
  111. value = [];
  112. var children = this.getChildren();
  113. for(var i in children){
  114. var child = children[i];
  115. if(child instanceof dojox.wire.ml.DataProperty){
  116. value.push(child.getValue());
  117. }
  118. }
  119. }else if(this.type == "object"){
  120. value = {};
  121. var children = this.getChildren();
  122. for(var i in children){
  123. var child = children[i];
  124. if((child instanceof dojox.wire.ml.DataProperty) && child.name){
  125. value[child.name] = child.getValue();
  126. }
  127. }
  128. }else if(this.type == "element"){
  129. value = new dojox.wire.ml.XmlElement(value);
  130. var children = this.getChildren();
  131. for(var i in children){
  132. var child = children[i];
  133. if((child instanceof dojox.wire.ml.DataProperty) && child.name){
  134. value.setPropertyValue(child.name, child.getValue());
  135. }
  136. }
  137. }
  138. }
  139. return value; //anything
  140. }
  141. });
  142. }