scriptFrame.js 3.7 KB

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