Invocation.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // wrapped by build app
  2. define("dojox/wire/ml/Invocation", ["dijit","dojo","dojox","dojo/require!dojox/wire/ml/Action"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.wire.ml.Invocation");
  4. dojo.require("dojox.wire.ml.Action");
  5. dojo.declare("dojox.wire.ml.Invocation", dojox.wire.ml.Action, {
  6. // summary:
  7. // A widget to invoke a method or publish a topic
  8. // description:
  9. // This widget represents a controller task to invoke a method or
  10. // publish a topic when an event (a function) or a topic is issued.
  11. // object:
  12. // A scope of a method to invoke
  13. // method:
  14. // A name of a method to invoke
  15. // topic:
  16. // A name of a topic to publish
  17. // parameters:
  18. // Arguments for the method or the topic
  19. // result:
  20. // A property to store a return value of the method call
  21. // error:
  22. // A property to store an error on the method call
  23. object: "",
  24. method: "",
  25. topic: "",
  26. parameters: "",
  27. result: "",
  28. error: "",
  29. _run: function(){
  30. // summary:
  31. // Invoke a method or publish a topic
  32. // description:
  33. // If 'topic' is specified, the topic is published with arguments
  34. // specified to 'parameters'.
  35. // If 'method' and 'object' are specified, the method is invoked
  36. // with arguments specified to 'parameters' and set the return
  37. // value to a property specified to 'result'.
  38. // 'object', 'parameters' and 'result' can specify properties of
  39. // a widget or an DOM element with the dotted notation.
  40. // If 'parameters' are omitted, the arguments to this method are
  41. // passed as is.
  42. if(this.topic){
  43. var args = this._getParameters(arguments);
  44. try{
  45. dojo.publish(this.topic, args);
  46. this.onComplete();
  47. }catch(e){
  48. this.onError(e);
  49. }
  50. }else if(this.method){
  51. var scope = (this.object ? dojox.wire.ml._getValue(this.object) : dojo.global);
  52. if(!scope){
  53. return; //undefined
  54. }
  55. var args = this._getParameters(arguments);
  56. var func = scope[this.method];
  57. if(!func){
  58. func = scope.callMethod;
  59. if(!func){
  60. return; //undefined
  61. }
  62. args = [this.method, args];
  63. }
  64. try{
  65. var connected = false;
  66. if(scope.getFeatures){
  67. var features = scope.getFeatures();
  68. if((this.method == "fetch" && features["dojo.data.api.Read"]) ||
  69. (this.method == "save" && features["dojo.data.api.Write"])){
  70. var arg = args[0];
  71. if(!arg.onComplete){
  72. arg.onComplete = function(){};
  73. }
  74. //dojo.connect(arg, "onComplete", this, "onComplete");
  75. this.connect(arg, "onComplete", "onComplete");
  76. if(!arg.onError){
  77. arg.onError = function(){};
  78. }
  79. //dojo.connect(arg, "onError", this, "onError");
  80. this.connect(arg, "onError", "onError");
  81. connected = true;
  82. }
  83. }
  84. var r = func.apply(scope, args);
  85. if(!connected){
  86. if(r && (r instanceof dojo.Deferred)){
  87. var self = this;
  88. r.addCallbacks(
  89. function(result){self.onComplete(result);},
  90. function(error){self.onError(error);}
  91. );
  92. }else{
  93. this.onComplete(r);
  94. }
  95. }
  96. }catch(e){
  97. this.onError(e);
  98. }
  99. }
  100. },
  101. onComplete: function(/*anything*/result){
  102. // summary:
  103. // A function called when the method or the topic publish
  104. // completed
  105. // description:
  106. // If 'result' attribute is specified, the result object also set
  107. // to the specified property.
  108. // result:
  109. // The return value of a method or undefined for a topic
  110. if(this.result){
  111. dojox.wire.ml._setValue(this.result, result);
  112. }
  113. if(this.error){ // clear error
  114. dojox.wire.ml._setValue(this.error, "");
  115. }
  116. },
  117. onError: function(/*anything*/error){
  118. // summary:
  119. // A function called on an error occurs
  120. // description:
  121. // If 'error' attribute is specified, the error object also set to
  122. // the specified property.
  123. // error:
  124. // The exception or error occurred
  125. if(this.error){
  126. if(error && error.message){
  127. error = error.message;
  128. }
  129. dojox.wire.ml._setValue(this.error, error);
  130. }
  131. },
  132. _getParameters: function(/*Array*/args){
  133. // summary:
  134. // Returns arguments to a method or topic to invoke
  135. // description:
  136. // This method retunrs an array of arguments specified by
  137. // 'parameters' attribute, a comma-separated list of IDs and
  138. // their properties in a dotted notation.
  139. // If 'parameters' are omitted, the original arguments are
  140. // used.
  141. // args:
  142. // Arguments to a trigger event or topic
  143. if(!this.parameters){
  144. // use arguments as is
  145. return args; //Array
  146. }
  147. var parameters = [];
  148. var list = this.parameters.split(",");
  149. if(list.length == 1){
  150. var parameter = dojox.wire.ml._getValue(dojo.trim(list[0]), args);
  151. if(dojo.isArray(parameter)){
  152. parameters = parameter;
  153. }else{
  154. parameters.push(parameter);
  155. }
  156. }else{
  157. for(var i in list){
  158. parameters.push(dojox.wire.ml._getValue(dojo.trim(list[i]), args));
  159. }
  160. }
  161. return parameters; //Array
  162. }
  163. });
  164. });