source: Dev/branches/rest-dojo-ui/client/dojox/io/scriptFrame.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: 3.4 KB
Line 
1define(["dojo/main", "dojo/io/script", "dojo/io/iframe"], function(dojo, ioScript, iframe){
2        dojo.deprecated("dojox.io.scriptFrame", "dojo.io.script now supports parallel requests without dojox.io.scriptFrame", "2.0");
3        dojo.getObject("io.scriptFrame", true, dojox);
4
5//This module extends dojo.io.script to use an iframe for the dojo.io.script.attach calls
6//if the frameDoc argument is passed to dojo.io.script.get(), and if frameDoc is a string (representing
7//the DOM ID of an iframe that should be used for the connection. If frameDoc is not a string, then
8//it is probably a document object, and dojox.io.scriptFrame should not get involved with the request.
9//This is useful in some long-polling comet situations in Firefox and Opera. Those browsers execute scripts
10//in DOM order, not network-receive order, so a long-polling script will block other
11//dynamically appended scripts from running until it completes. By using an iframe
12//for the dojo.io.script requests, this issue can be avoided.
13
14//WARNING: the url argument to dojo.io.script MUST BE relative to the iframe document's location,
15//NOT the parent page location. This iframe document's URL will be (dojo.moduleUrl("dojo", "resources/blank.html")
16//or djConfig.dojoBlankHtmlUrl (for xdomain loading).
17
18        dojox.io.scriptFrame = {
19                _waiters: {},
20                _loadedIds: {},
21
22                _getWaiters: function(/*String*/frameId){
23                        return this._waiters[frameId] || (this._waiters[frameId] = []);
24                },
25
26                _fixAttachUrl: function(/*String*/url){
27                        //summary: fixes the URL so that
28                },
29
30                _loaded: function(/*String*/frameId){
31                        //summary: callback used when waiting for a frame to load (related to the usage of
32                        //the frameId argument to dojo.io.script.get().
33                        var waiters = this._getWaiters(frameId);
34                        this._loadedIds[frameId] = true;
35                        this._waiters[frameId] = null;
36
37                        for(var i = 0; i < waiters.length; i++){
38                                var ioArgs = waiters[i];
39                                ioArgs.frameDoc = iframe.doc(dojo.byId(frameId));
40                                ioScript.attach(ioArgs.id, ioArgs.url, ioArgs.frameDoc);
41                        }
42                }
43        };
44
45        //Hold on to the old _canAttach function.
46        var oldCanAttach = ioScript._canAttach;
47        var scriptFrame = dojox.io.scriptFrame;
48
49        //Define frame-aware _canAttach method on dojo.io.script
50        ioScript._canAttach = function(/*Object*/ioArgs){
51                //summary: provides an override of dojo.io.script._canAttach to check for
52                //the existence of a the args.frameDoc property. If it is there, and it is a string,
53                //not a document, then create the iframe with an ID of frameDoc, and use that for the calls.
54                //If frameDoc is a document, then dojox.io.scriptFrame should not get involved.
55                var fId = ioArgs.args.frameDoc;
56
57                if(fId && dojo.isString(fId)){
58                        var frame = dojo.byId(fId);
59                        var waiters = scriptFrame._getWaiters(fId);
60                        if(!frame){
61                                //Need to create frame, but the frame document, which *must* be
62                                //on the same domain as the page (set djConfig.dojoBlankHtmlUrl
63                                //if using xdomain loading). Loading of the frame document is asynchronous,
64                                //so we need to do callback stuff.
65                                waiters.push(ioArgs);
66                                iframe.create(fId, dojox._scopeName + ".io.scriptFrame._loaded('" + fId + "');");
67                        }else{
68                                //Frame loading could still be happening. Only call attach if the frame has loaded.
69                                if(scriptFrame._loadedIds[fId]){
70                                        ioArgs.frameDoc = iframe.doc(frame);
71                                        this.attach(ioArgs.id, ioArgs.url, ioArgs.frameDoc);
72                                }else{
73                                        waiters.push(ioArgs);
74                                }
75                        }
76                        return false;
77                }else{
78                        return oldCanAttach.apply(this, arguments);
79                }
80        };
81
82        return dojox.io.scriptFrame;
83});
84
Note: See TracBrowser for help on using the repository browser.