root / trunk / web / dojo / dojox / wire / ml / Service.js
History | View | Annotate | Download (3.84 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.wire.ml.Service"]){ |
9 |
dojo._hasResource["dojox.wire.ml.Service"]=true; |
10 |
dojo.provide("dojox.wire.ml.Service");
|
11 |
dojo.provide("dojox.wire.ml.RestHandler");
|
12 |
dojo.provide("dojox.wire.ml.XmlHandler");
|
13 |
dojo.provide("dojox.wire.ml.JsonHandler");
|
14 |
dojo.require("dijit._Widget");
|
15 |
dojo.require("dojox.xml.parser");
|
16 |
dojo.require("dojox.wire._base");
|
17 |
dojo.require("dojox.wire.ml.util");
|
18 |
dojo.declare("dojox.wire.ml.Service",dijit._Widget,{url:"",serviceUrl:"",serviceType:"",handlerClass:"",preventCache:true,postCreate:function(){ |
19 |
this.handler=this._createHandler(); |
20 |
},_handlerClasses:{"TEXT":"dojox.wire.ml.RestHandler","XML":"dojox.wire.ml.XmlHandler","JSON":"dojox.wire.ml.JsonHandler","JSON-RPC":"dojo.rpc.JsonService"},_createHandler:function(){ |
21 |
if(this.url){ |
22 |
var _1=this; |
23 |
var d=dojo.xhrGet({url:this.url,handleAs:"json",sync:true}); |
24 |
d.addCallback(function(_2){
|
25 |
_1.smd=_2; |
26 |
}); |
27 |
if(this.smd&&!this.serviceUrl){ |
28 |
this.serviceUrl=(this.smd.serviceUrl||this.smd.serviceURL); |
29 |
} |
30 |
} |
31 |
var _3=undefined; |
32 |
if(this.handlerClass){ |
33 |
_3=dojox.wire._getClass(this.handlerClass);
|
34 |
}else{
|
35 |
if(this.serviceType){ |
36 |
_3=this._handlerClasses[this.serviceType]; |
37 |
if(_3&&dojo.isString(_3)){
|
38 |
_3=dojox.wire._getClass(_3); |
39 |
this._handlerClasses[this.serviceType]=_3; |
40 |
} |
41 |
}else{
|
42 |
if(this.smd&&this.smd.serviceType){ |
43 |
_3=this._handlerClasses[this.smd.serviceType]; |
44 |
if(_3&&dojo.isString(_3)){
|
45 |
_3=dojox.wire._getClass(_3); |
46 |
this._handlerClasses[this.smd.serviceType]=_3; |
47 |
} |
48 |
} |
49 |
} |
50 |
} |
51 |
if(!_3){
|
52 |
return null; |
53 |
} |
54 |
return new _3(); |
55 |
},callMethod:function(_4,_5){ |
56 |
var _6=new dojo.Deferred(); |
57 |
this.handler.bind(_4,_5,_6,this.serviceUrl); |
58 |
return _6;
|
59 |
}}); |
60 |
dojo.declare("dojox.wire.ml.RestHandler",null,{contentType:"text/plain",handleAs:"text",bind:function(_7,_8,_9,_a){ |
61 |
_7=_7.toUpperCase(); |
62 |
var _b=this; |
63 |
var _c={url:this._getUrl(_7,_8,_a),contentType:this.contentType,handleAs:this.handleAs,headers:this.headers,preventCache:this.preventCache}; |
64 |
var d=null; |
65 |
if(_7=="POST"){ |
66 |
_c.postData=this._getContent(_7,_8);
|
67 |
d=dojo.rawXhrPost(_c); |
68 |
}else{
|
69 |
if(_7=="PUT"){ |
70 |
_c.putData=this._getContent(_7,_8);
|
71 |
d=dojo.rawXhrPut(_c); |
72 |
}else{
|
73 |
if(_7=="DELETE"){ |
74 |
d=dojo.xhrDelete(_c); |
75 |
}else{
|
76 |
d=dojo.xhrGet(_c); |
77 |
} |
78 |
} |
79 |
} |
80 |
d.addCallbacks(function(_d){
|
81 |
_9.callback(_b._getResult(_d)); |
82 |
},function(_e){
|
83 |
_9.errback(_e); |
84 |
}); |
85 |
},_getUrl:function(_f,_10,url){ |
86 |
var _11;
|
87 |
if(_f=="GET"||_f=="DELETE"){ |
88 |
if(_10.length>0){ |
89 |
_11=_10[0];
|
90 |
} |
91 |
}else{
|
92 |
if(_10.length>1){ |
93 |
_11=_10[1];
|
94 |
} |
95 |
} |
96 |
if(_11){
|
97 |
var _12=""; |
98 |
for(var _13 in _11){ |
99 |
var _14=_11[_13];
|
100 |
if(_14){
|
101 |
_14=encodeURIComponent(_14); |
102 |
var _15="{"+_13+"}"; |
103 |
var _16=url.indexOf(_15);
|
104 |
if(_16>=0){ |
105 |
url=url.substring(0,_16)+_14+url.substring(_16+_15.length);
|
106 |
}else{
|
107 |
if(_12){
|
108 |
_12+="&";
|
109 |
} |
110 |
_12+=(_13+"="+_14);
|
111 |
} |
112 |
} |
113 |
} |
114 |
if(_12){
|
115 |
url+="?"+_12;
|
116 |
} |
117 |
} |
118 |
return url;
|
119 |
},_getContent:function(_17,_18){ |
120 |
if(_17=="POST"||_17=="PUT"){ |
121 |
return (_18?_18[0]:null); |
122 |
}else{
|
123 |
return null; |
124 |
} |
125 |
},_getResult:function(_19){ |
126 |
return _19;
|
127 |
}}); |
128 |
dojo.declare("dojox.wire.ml.XmlHandler",dojox.wire.ml.RestHandler,{contentType:"text/xml",handleAs:"xml",_getContent:function(_1a,_1b){ |
129 |
var _1c=null; |
130 |
if(_1a=="POST"||_1a=="PUT"){ |
131 |
var p=_1b[0]; |
132 |
if(p){
|
133 |
if(dojo.isString(p)){
|
134 |
_1c=p; |
135 |
}else{
|
136 |
var _1d=p;
|
137 |
if(_1d instanceof dojox.wire.ml.XmlElement){ |
138 |
_1d=_1d.element; |
139 |
}else{
|
140 |
if(_1d.nodeType===9){ |
141 |
_1d=_1d.documentElement; |
142 |
} |
143 |
} |
144 |
var _1e="<?xml version=\"1.0\"?>"; |
145 |
_1c=_1e+dojox.xml.parser.innerXML(_1d); |
146 |
} |
147 |
} |
148 |
} |
149 |
return _1c;
|
150 |
},_getResult:function(_1f){ |
151 |
if(_1f){
|
152 |
_1f=new dojox.wire.ml.XmlElement(_1f);
|
153 |
} |
154 |
return _1f;
|
155 |
}}); |
156 |
dojo.declare("dojox.wire.ml.JsonHandler",dojox.wire.ml.RestHandler,{contentType:"text/json",handleAs:"json",headers:{"Accept":"*/json"},_getContent:function(_20,_21){ |
157 |
var _22=null; |
158 |
if(_20=="POST"||_20=="PUT"){ |
159 |
var p=(_21?_21[0]:undefined); |
160 |
if(p){
|
161 |
if(dojo.isString(p)){
|
162 |
_22=p; |
163 |
}else{
|
164 |
_22=dojo.toJson(p); |
165 |
} |
166 |
} |
167 |
} |
168 |
return _22;
|
169 |
}}); |
170 |
} |