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

Last change on this file since 274 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: 2.7 KB
Line 
1dojo.provide("dojox.wire.ml.Service");
2
3dojo.require("dijit._Widget");
4dojo.require("dojox.xml.parser");
5dojo.require("dojox.wire._base");
6dojo.require("dojox.wire.ml.util");
7
8dojo.declare("dojox.wire.ml.Service", dijit._Widget, {
9        //      summary:
10        //              A widget for a service
11        //      description:
12        //              This widget represents a service defined by a service description
13        //              specified with 'url' attribute.
14        //              If 'serviceType' and 'serviceUrl' attributes are specified, 'url'
15        //              attribute can be omitted.
16        //      url:
17        //              A URL to a service description
18        //      serviceUrl:
19        //              A URL to a service
20        //      serviceType:
21        //              A service type
22        //      handlerClass:
23        //              A service handler class name
24        url: "",
25        serviceUrl: "",
26        serviceType: "",
27        handlerClass: "",
28        preventCache: true,
29
30        postCreate: function(){
31                //      summary:
32                //              Call _createHandler()
33                //      description:
34                //              See _createHandler().
35                this.handler = this._createHandler();
36        },
37
38        _handlerClasses: {
39                "TEXT": "dojox.wire.ml.RestHandler",
40                "XML": "dojox.wire.ml.XmlHandler",
41                "JSON": "dojox.wire.ml.JsonHandler",
42                "JSON-RPC": "dojo.rpc.JsonService"
43        },
44
45        _createHandler: function(){
46                //      summary:
47                //              Create a service handler
48                //      desription:
49                //              A service handler class is determined by:
50                //              1. 'handlerClass' attribute
51                //              2. 'serviceType' attribute
52                //              3. 'serviceType' property in a service description
53                //      returns:
54                //              A service handler
55                if(this.url){
56                        var self = this;
57                        var d = dojo.xhrGet({
58                                url: this.url,
59                                handleAs: "json",
60                                sync: true
61                        });
62                        d.addCallback(function(result){
63                                self.smd = result;
64                        });
65                        if(this.smd && !this.serviceUrl){
66                                this.serviceUrl = (this.smd.serviceUrl || this.smd.serviceURL);
67                        }
68                }
69                var handlerClass = undefined;
70                if(this.handlerClass){
71                        handlerClass = dojox.wire._getClass(this.handlerClass);
72                }else if(this.serviceType){
73                        handlerClass = this._handlerClasses[this.serviceType];
74                        if(handlerClass && dojo.isString(handlerClass)){
75                                handlerClass = dojox.wire._getClass(handlerClass);
76                                this._handlerClasses[this.serviceType] = handlerClass;
77                        }
78                }else if(this.smd && this.smd.serviceType){
79                        handlerClass = this._handlerClasses[this.smd.serviceType];
80                        if(handlerClass && dojo.isString(handlerClass)){
81                                handlerClass = dojox.wire._getClass(handlerClass);
82                                this._handlerClasses[this.smd.serviceType] = handlerClass;
83                        }
84                }
85                if(!handlerClass){
86                        return null; //null
87                }
88                return new handlerClass(); //Object
89        },
90
91        callMethod: function(method, parameters){
92                //      summary:
93                //              Call a service method with parameters
94                //      method:
95                //              A method name
96                //      parameters:
97                //              An array parameters
98                var deferred = new dojo.Deferred();
99                this.handler.bind(method, parameters, deferred, this.serviceUrl);
100                return deferred;
101        }
102});
Note: See TracBrowser for help on using the repository browser.