Invocation.js 5.1 KB

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