Data.js 4.1 KB

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