]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/io/script.js.uncompressed.js
1 define("dojo/io/script", [
2 "../_base/connect", /*===== "../_base/declare", =====*/ "../_base/kernel", "../_base/lang",
3 "../sniff", "../_base/window","../_base/xhr",
4 "../dom", "../dom-construct", "../request/script"
5 ], function(connect
, /*===== declare, =====*/ kernel
, lang
, has
, win
, xhr
, dom
, domConstruct
, _script
){
10 kernel
.deprecated("dojo/io/script", "Use dojo/request/script.", "2.0");
13 var __ioArgs = declare(kernel.__IoArgs, {
15 // All the properties described in the dojo.__ioArgs type, apply to this
16 // type as well, EXCEPT "handleAs". It is not applicable to
17 // dojo/io/script.get() calls, since it is implied by the usage of
18 // "jsonp" (response will be a JSONP call returning JSON)
19 // or the response is pure JavaScript defined in
20 // the body of the script that was attached.
21 // callbackParamName: String
22 // Deprecated as of Dojo 1.4 in favor of "jsonp", but still supported for
23 // legacy code. See notes for jsonp property.
25 // The URL parameter name that indicates the JSONP callback string.
26 // For instance, when using Yahoo JSONP calls it is normally,
27 // jsonp: "callback". For AOL JSONP calls it is normally
29 // checkString: String
30 // A string of JavaScript that when evaluated like so:
31 // "typeof(" + checkString + ") != 'undefined'"
32 // being true means that the script fetched has been loaded.
33 // Do not use this if doing a JSONP type of call (use callbackParamName instead).
35 // The Document object for a child iframe. If this is passed in, the script
36 // will be attached to that document. This can be helpful in some comet long-polling
37 // scenarios with Firefox and Opera.
45 get: function(/*__ioArgs*/ args
){
47 // sends a get request using a dynamically created script tag.
49 var dfd
= this._makeScriptDeferred(args
, function(dfd
){
50 rDfd
&& rDfd
.cancel();
52 var ioArgs
= dfd
.ioArgs
;
53 xhr
._ioAddQueryToUrl(ioArgs
);
55 xhr
._ioNotifyStart(dfd
);
57 rDfd
= _script
.get(ioArgs
.url
, {
58 timeout
: args
.timeout
,
60 checkString
: args
.checkString
,
62 frameDoc
: args
.frameDoc
,
63 canAttach: function(rDfd
){
65 ioArgs
.requestId
= rDfd
.id
;
66 ioArgs
.scriptId
= rDfd
.scriptId
;
67 ioArgs
.canDelete
= rDfd
.canDelete
;
69 return script
._canAttach(ioArgs
);
75 }).otherwise(function(error
){
76 dfd
.ioArgs
.error
= error
;
83 attach
: _script
._attach
,
84 remove
: _script
._remove
,
86 _makeScriptDeferred: function(/*Object*/ args
, /*Function?*/ cancel
){
88 // sets up a Deferred object for an IO request.
89 var dfd
= xhr
._ioSetArgs(args
, cancel
|| this._deferredCancel
, this._deferredOk
, this._deferredError
);
91 var ioArgs
= dfd
.ioArgs
;
92 ioArgs
.id
= kernel
._scopeName
+ "IoScript" + (this._counter
++);
93 ioArgs
.canDelete
= false;
95 //Special setup for jsonp case
96 ioArgs
.jsonp
= args
.callbackParamName
|| args
.jsonp
;
98 //Add the jsonp parameter.
99 ioArgs
.query
= ioArgs
.query
|| "";
100 if(ioArgs
.query
.length
> 0){
103 ioArgs
.query
+= ioArgs
.jsonp
+
104 "=" + (args
.frameDoc
? "parent." : "") +
105 kernel
._scopeName
+ ".io.script.jsonp_" + ioArgs
.id
+ "._jsonpCallback";
107 ioArgs
.frameDoc
= args
.frameDoc
;
109 //Setup the Deferred to have the jsonp callback.
110 ioArgs
.canDelete
= true;
111 dfd
._jsonpCallback
= this._jsonpCallback
;
112 this["jsonp_" + ioArgs
.id
] = dfd
;
114 return dfd
; // dojo/_base/Deferred
117 _deferredCancel: function(/*Deferred*/ dfd
){
119 // canceller function for xhr._ioSetArgs call.
121 //DO NOT use "this" and expect it to be script.
125 _deferredOk: function(/*Deferred*/ dfd
){
127 // okHandler function for xhr._ioSetArgs call.
129 //DO NOT use "this" and expect it to be script.
130 var ioArgs
= dfd
.ioArgs
;
132 //Favor JSONP responses, script load events then lastly ioArgs.
133 //The ioArgs are goofy, but cannot return the dfd since that stops
134 //the callback chain in Deferred. The return value is not that important
135 //in that case, probably a checkString case.
136 return ioArgs
.json
|| ioArgs
.scriptLoaded
|| ioArgs
;
139 _deferredError: function(/*Error*/ error
, /*Deferred*/ dfd
){
141 // errHandler function for xhr._ioSetArgs call.
143 console
.log("dojo.io.script error", error
);
150 _addDeadScript: function(/*Object*/ ioArgs
){
152 // sets up an entry in the deadScripts array.
153 script
._deadScripts
.push({id
: ioArgs
.id
, frameDoc
: ioArgs
.frameDoc
});
154 //Being extra paranoid about leaks:
155 ioArgs
.frameDoc
= null;
158 _validCheck: function(/*Deferred*/ dfd
){
160 // inflight check function to see if dfd is still valid.
162 // TODO: why isn't dfd accessed?
164 //Do script cleanup here. We wait for one inflight pass
165 //to make sure we don't get any weird things by trying to remove a script
166 //tag that is part of the call chain (IE 6 has been known to
167 //crash in that case).
168 var deadScripts
= script
._deadScripts
;
169 if(deadScripts
&& deadScripts
.length
> 0){
170 for(var i
= 0; i
< deadScripts
.length
; i
++){
171 //Remove the script tag
172 script
.remove(deadScripts
[i
].id
, deadScripts
[i
].frameDoc
);
173 deadScripts
[i
].frameDoc
= null;
175 script
._deadScripts
= [];
181 _ioCheck: function(dfd
){
183 // inflight check function to see if IO finished.
185 var ioArgs
= dfd
.ioArgs
;
186 //Check for finished jsonp
187 if(ioArgs
.json
|| (ioArgs
.scriptLoaded
&& !ioArgs
.args
.checkString
)){
191 //Check for finished "checkString" case.
192 var checkString
= ioArgs
.args
.checkString
;
193 return checkString
&& eval("typeof(" + checkString
+ ") != 'undefined'");
198 _resHandle: function(/*Deferred*/ dfd
){
200 // inflight function to handle a completed response.
201 if(script
._ioCheck(dfd
)){
204 //This path should never happen since the only way we can get
205 //to _resHandle is if _ioCheck is true.
206 dfd
.errback(new Error("inconceivable dojo.io.script._resHandle error"));
210 _canAttach: function(/*===== ioArgs =====*/ ){
212 // A method that can be overridden by other modules
213 // to control when the script attachment occurs.
218 _jsonpCallback: function(/*JSON Object*/ json
){
220 // generic handler for jsonp callback. A pointer to this function
221 // is used for all jsonp callbacks. NOTE: the "this" in this
222 // function will be the Deferred object that represents the script
224 this.ioArgs
.json
= json
;
225 kernel
.global
[_script
._callbacksProperty
][this.ioArgs
.requestId
](json
);
229 lang
.setObject("dojo.io.script", script
);
232 script.attach = function(id, url, frameDocument){
234 // creates a new `<script>` tag pointing to the specified URL and
235 // adds it to the document.
237 // Attaches the script element to the DOM. Use this method if you
238 // just want to attach a script to the DOM and do not care when or
241 script.remove = function(id, frameDocument){
243 // removes the script element with the given id, from the given frameDocument.
244 // If no frameDocument is passed, the current document is used.