Invocation.js 5.0 KB

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