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