source: Dev/trunk/src/client/dojox/wire/ml/Invocation.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 4.6 KB
Line 
1dojo.provide("dojox.wire.ml.Invocation");
2
3dojo.require("dojox.wire.ml.Action");
4
5dojo.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
12        // object:
13        //              A scope of a method to invoke
14        object: "",
15
16        // method:
17        //              A name of a method to invoke
18        method: "",
19
20        // topic:
21        //              A name of a topic to publish
22        topic: "",
23
24        // parameters:
25        //              Arguments for the method or the topic
26        parameters: "",
27
28        // result:
29        //              A property to store a return value of the method call
30        result: "",
31
32        // error:
33        //              A property to store an error on the method call
34        error: "",
35
36        _run: function(){
37                // summary:
38                //              Invoke a method or publish a topic
39                // description:
40                //              If 'topic' is specified, the topic is published with arguments
41                //              specified to 'parameters'.
42                //              If 'method' and 'object' are specified, the method is invoked
43                //              with arguments specified to 'parameters' and set the return
44                //              value to a property specified to 'result'.
45                //              'object', 'parameters' and 'result' can specify properties of
46                //              a widget or an DOM element with the dotted notation.
47                //              If 'parameters' are omitted, the arguments to this method are
48                //              passed as is.
49                if(this.topic){
50                        var args = this._getParameters(arguments);
51                        try{
52                                dojo.publish(this.topic, args);
53                                this.onComplete();
54                        }catch(e){
55                                this.onError(e);
56                        }
57                }else if(this.method){
58                        var scope = (this.object ? dojox.wire.ml._getValue(this.object) : dojo.global);
59                        if(!scope){
60                                return; //undefined
61                        }
62                        var args = this._getParameters(arguments);
63                        var func = scope[this.method];
64                        if(!func){
65                                func = scope.callMethod;
66                                if(!func){
67                                        return; //undefined
68                                }
69                                args = [this.method, args];
70                        }
71                        try{
72                                var connected = false;
73                                if(scope.getFeatures){
74                                        var features = scope.getFeatures();
75                                        if((this.method == "fetch" && features["dojo.data.api.Read"]) ||
76                                                (this.method == "save" && features["dojo.data.api.Write"])){
77                                                var arg = args[0];
78                                                if(!arg.onComplete){
79                                                        arg.onComplete = function(){};
80                                                }
81                                                //dojo.connect(arg, "onComplete", this, "onComplete");
82                                                this.connect(arg, "onComplete", "onComplete");
83                                                if(!arg.onError){
84                                                        arg.onError = function(){};
85                                                }
86                                                //dojo.connect(arg, "onError", this, "onError");
87                                                this.connect(arg, "onError", "onError");
88                                                connected = true;
89                                        }
90                                }
91                                var r = func.apply(scope, args);
92                                if(!connected){
93                                        if(r && (r instanceof dojo.Deferred)){
94                                                var self = this;
95                                                r.addCallbacks(
96                                                        function(result){self.onComplete(result);},
97                                                        function(error){self.onError(error);}
98                                                );
99                                        }else{
100                                                this.onComplete(r);
101                                        }
102                                }
103                        }catch(e){
104                                this.onError(e);
105                        }
106                }
107        },
108
109        onComplete: function(/*anything*/result){
110                // summary:
111                //              A function called when the method or the topic publish
112                //              completed
113                // description:
114                //              If 'result' attribute is specified, the result object also set
115                //              to the specified property.
116                // result:
117                //              The return value of a method or undefined for a topic
118                if(this.result){
119                        dojox.wire.ml._setValue(this.result, result);
120                }
121                if(this.error){ // clear error
122                        dojox.wire.ml._setValue(this.error, "");
123                }
124        },
125
126        onError: function(/*anything*/error){
127                // summary:
128                //              A function called on an error occurs
129                // description:
130                //              If 'error' attribute is specified, the error object also set to
131                //              the specified property.
132                // error:
133                //              The exception or error occurred
134                if(this.error){
135                        if(error && error.message){
136                                error = error.message;
137                        }
138                        dojox.wire.ml._setValue(this.error, error);
139                }
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(dojo.trim(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(dojo.trim(list[i]), args));
169                        }
170                }
171                return parameters; //Array
172        }
173});
Note: See TracBrowser for help on using the repository browser.