Dojo - Issue loading widget cross-domain

谁都会走 提交于 2019-12-11 18:12:30

问题


I'm working on a project that requires that some custom Dojo widgets (i.e., widgets we have written ourselves) are loaded from another server. Despite my best efforts over several days, I cannot seem to get Dojo to load the widgets.

Dojo is loaded from the Google CDN, the widget is loaded from www.example.com, and the website is located at www.foo.com.

I cannot post the actual project files (this is a project for a company), but I have reproduced the error with smaller test files.

Test.html (on www.foo.com):

<html>

<div id="content"></div>

<script>
    var djConfig = {
        isDebug: true,
        modulePaths: {
            'com.example': 'http://example.com/some/path/com.example'
        }
    }
</script>

<script src="https://ajax.googleapis.com/ajax/libs/dojo/1.4.3/dojo/dojo.xd.js.uncompressed.js"></script>

<script type="text/javascript">
    dojo.require("dijit._Widget");
    dojo.require("dijit._Templated");

    dojo.addOnLoad(function() {
        dojo.require("com.example.widget.Test", false);

        dojo.addOnLoad(function() {
            new com.example.widget.Test().placeAt(dojo.byId('content'));
        });
    });
</script>

</html>

Test.xd.js (at www.example.com/some/path/com.example/widget/Test.xd.js):

dojo.provide("com.example.widget.Test");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.declare("com.example.widget.Test", [dijit._Widget, dijit._Templated], {
    templateString: "<div dojoAttachPoint=\"div\">This is a test</div>",

    postCreate: function() {
        console.log("In postCreate");
        console.log(this.div);
        this.div.innerHTML += '!!!';
    }
});

In Firebug, I am seeing an error after a delay of a few seconds saying that the cross-domain resource com.example.widget.Test cannot be loaded. However, in the 'Net' tab I am able to see that Test.xd.js is successfully downloaded, and I am able to set a breakpoint and see that the dojo.declare executes and completes without error.

I appreciate any help. Please let me know if there is any other information I can provide.


回答1:


There is a different way for handling the module declarations in XD-loader. This is due to how the loader handles 'module-ready' event. You will most likely experience, that the dojo.addOnLoad never runs, since it 'knows' that certainly - some required modules are not declared.

Even so, they may very well be declared - and the change in 1.7+ versions of dojotoolkit seem to reckognize that fact. The reason for this, i believe, is that the mechanism for 'module-ready' is not implemented correctly in your myModule.xd.js modules.

It is basically a 'header' or 'closure' of the declaration, involving a few steps - wrapping everything in your basic module from dojo.provide and eof

Standard example boiler module file '{{modulePath}}/my/Tree.js'

dojo.provide("my.Tree");

dojo.require("dijit.Tree");

dojo.declare("my.Tree", dijit.Tree, {
    // class definition
});

X-Domain example boiler module file '{{modulePath}}/my/Tree.xd.js

dojo._xdResourceLoaded(function(){ 
  return {
    depends: [
        ["provide", "my.Tree"],
        ["require", "dijit.Tree"]
    ],
    defineResource: function(dojo) {
      ///////////////////////////////
      /// Begin standard declaration
         dojo.provide("my.Tree");

         dojo.require("dijit.Tree");

         dojo.declare("my.Tree", dijit.Tree, {
            // class definition
         });
      /// End standard declaration
      ///////////////////////////////
    }
  }
})();


来源:https://stackoverflow.com/questions/11633973/dojo-issue-loading-widget-cross-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!