_base.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // wrapped by build app
  2. define("dojox/wire/_base", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.wire._base");
  4. dojox.wire._defaultWireClass = "dojox.wire.Wire";
  5. dojox.wire._wireClasses = {
  6. "attribute": "dojox.wire.DataWire",
  7. "path": "dojox.wire.XmlWire",
  8. "children": "dojox.wire.CompositeWire",
  9. "columns": "dojox.wire.TableAdapter",
  10. "nodes": "dojox.wire.TreeAdapter",
  11. "segments": "dojox.wire.TextAdapter"
  12. };
  13. dojox.wire.register = function(/*Function||String*/wireClass, /*String*/key){
  14. // summary:
  15. // Register a Wire class
  16. // desription:
  17. // The specified Wire class or a class name is registered with
  18. // a key property of arguments to create a Wire
  19. // wireClass:
  20. // A class or full qualified class name
  21. // key:
  22. // A key property of arguments to create a Wire
  23. if(!wireClass || !key){
  24. return; //undefined
  25. }
  26. if(dojox.wire._wireClasses[key]){ // key already in use
  27. return; //undefined
  28. }
  29. dojox.wire._wireClasses[key] = wireClass;
  30. };
  31. dojox.wire._getClass = function(/*String*/name){
  32. // summary:
  33. // Returns a class
  34. // description:
  35. // The class is loaded by dojo.require() and returned
  36. // by dojo.getObject().
  37. // name:
  38. // A class name
  39. // returns:
  40. // A class
  41. dojo["require"](name); // use dojo["require"] instead of dojo.require to avoid a build problem
  42. return dojo.getObject(name); //Function
  43. };
  44. dojox.wire.create = function(/*Object*/args){
  45. // summary:
  46. // Create a Wire from arguments
  47. // description:
  48. // If 'args' specifies 'wireClass', it is used as a class or full
  49. // qualified class name to create a Wire with 'args' as arguments.
  50. // Otherwise, a Wire class is determined by other proeprties of 'args'
  51. // checking if 'args' specifies a key property for a Wire class.
  52. // If no key property found, the default Wire class is used.
  53. // args:
  54. // Arguments to create a Wire
  55. // returns:
  56. // A Wire
  57. if(!args){
  58. args = {};
  59. }
  60. var wireClass = args.wireClass;
  61. if(wireClass){
  62. if(dojo.isString(wireClass)){
  63. wireClass = dojox.wire._getClass(wireClass);
  64. }
  65. }else{
  66. for(var key in args){
  67. if(!args[key]){
  68. continue;
  69. }
  70. wireClass = dojox.wire._wireClasses[key];
  71. if(wireClass){
  72. if(dojo.isString(wireClass)){
  73. wireClass = dojox.wire._getClass(wireClass);
  74. dojox.wire._wireClasses[key] = wireClass;
  75. }
  76. break;
  77. }
  78. }
  79. }
  80. if(!wireClass){
  81. if(dojo.isString(dojox.wire._defaultWireClass)){
  82. dojox.wire._defaultWireClass = dojox.wire._getClass(dojox.wire._defaultWireClass);
  83. }
  84. wireClass = dojox.wire._defaultWireClass;
  85. }
  86. return new wireClass(args); //Object
  87. };
  88. dojox.wire.isWire = function(/*Object*/wire){
  89. // summary:
  90. // Check if an object is a Wire
  91. // description:
  92. // If the specified object is a Wire, true is returned.
  93. // Otherwise, false is returned.
  94. // wire:
  95. // An object to check
  96. // returns:
  97. // True if the object is a Wire, otherwise false
  98. return (wire && wire._wireClass); //Boolean
  99. };
  100. dojox.wire.transfer = function(/*Wire||Object*/source, /*Wire||Object*/target, /*Object?*/defaultObject, /*Object?*/defaultTargetObject){
  101. // summary:
  102. // Transfer a source value to a target value
  103. // description:
  104. // If 'source' and/or 'target' are not Wires, Wires are created with
  105. // them as arguments.
  106. // A value is got through the source Wire and set through the target
  107. // Wire.
  108. // 'defaultObject' is passed to Wires as a default root object.
  109. // If 'defaultTargetObject' is specified, it is passed to the target
  110. // Wire as a default root object, instead of 'defaultObject'.
  111. // source:
  112. // A Wire or arguments to create a Wire for a source value
  113. // target:
  114. // A Wire or arguments to create a Wire for a target value
  115. // defaultObject:
  116. // defaultTargetObject;
  117. // Optional default root objects passed to Wires
  118. if(!source || !target){
  119. return; //undefined
  120. }
  121. if(!dojox.wire.isWire(source)){
  122. source = dojox.wire.create(source);
  123. }
  124. if(!dojox.wire.isWire(target)){
  125. target = dojox.wire.create(target);
  126. }
  127. var value = source.getValue(defaultObject);
  128. target.setValue(value, (defaultTargetObject || defaultObject));
  129. };
  130. dojox.wire.connect = function(/*Object*/trigger, /*Wire||Object*/source, /*Wire||Object*/target){
  131. // summary:
  132. // Transfer a source value to a target value on a trigger event or
  133. // topic
  134. // description:
  135. // If 'trigger' specifies 'topic', the topic is subscribed to transer
  136. // a value on the topic.
  137. // Otherwise, the event specified to 'event' of 'trigger' is listened
  138. // to transfer a value.
  139. // On the specified event or topic, transfer() is called with
  140. // 'source', 'target' and the arguments of the event or topic (as
  141. // default root objects).
  142. // trigger:
  143. // An event or topic to trigger a transfer
  144. // source:
  145. // A Wire or arguments to create a Wire for a source value
  146. // target:
  147. // A Wire or arguments to create a Wire for a target value
  148. // returns:
  149. // A connection handle for disconnect()
  150. if(!trigger || !source || !target){
  151. return; //undefined
  152. }
  153. var connection = {topic: trigger.topic};
  154. if(trigger.topic){
  155. connection.handle = dojo.subscribe(trigger.topic, function(){
  156. dojox.wire.transfer(source, target, arguments);
  157. });
  158. }else if(trigger.event){
  159. connection.handle = dojo.connect(trigger.scope, trigger.event, function(){
  160. dojox.wire.transfer(source, target, arguments);
  161. });
  162. }
  163. return connection; //Object
  164. };
  165. dojox.wire.disconnect = function(/*Object*/connection){
  166. // summary:
  167. // Remove a connection or subscription for transfer
  168. // description:
  169. // If 'handle' has 'topic', the topic is unsubscribed.
  170. // Otherwise, the listener to an event is removed.
  171. // connection:
  172. // A connection handle returned by connect()
  173. if(!connection || !connection.handle){
  174. return; //undefined
  175. }
  176. if(connection.topic){
  177. dojo.unsubscribe(connection.handle);
  178. }else{
  179. dojo.disconnect(connection.handle);
  180. }
  181. };
  182. });