source: Dev/branches/rest-dojo-ui/client/dojox/wire/ml/Invocation.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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