scriptFrame.js 3.5 KB

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