问题加载窗口小部件跨域

问题加载窗口小部件跨域

本文介绍了Dojo - 问题加载窗口小部件跨域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,需要一些自定义的Dojo小部件(即我们自己编写的小部件)从另一个服务器加载。尽管我在几天的努力,我似乎​​不能得到Dojo加载小部件。

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从Google CDN加载,小部件从www.example .com,网站位于www.foo.com。

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(在www.foo.com上):

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(在www.example.com/some/path/com.example/ widget / Test.xd.js):

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 += '!!!';
    }
});

在Firebug中,我看到一个错误,延迟几秒钟后说,跨域无法加载资源com.example.widget.Test。但是,在网络选项卡中,我可以看到Test.xd.js已成功下载,我可以设置一个断点,看到dojo.declare执行和完成没有错误。

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.

我很感激任何帮助。

推荐答案

有一种不同的方式来处理模块声明XD加载器。这是由于加载器如何处理模块就绪事件。你很可能会遇到,dojo.addOnLoad从不运行,因为它'知道'肯定 - 一些必需的模块没有声明。

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.

即使如此,他们可能非常良好的声明 - 和1.7+版本的dojotoolkit的变化似乎重新认识到这一事实。我相信的原因是,模块就绪的机制没有在您的myModule.xd.js模块中正确实现。

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.

它基本上是一个'header'或'closure'的声明,涉及几个步骤 - 包装你的基本模块中的所有东西从 dojo.provide eof

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

dojo.provide("my.Tree");

dojo.require("dijit.Tree");

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



X-Domain示例锅炉模块文件'{{modulePath}} / my / Tree.xd.js



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
      ///////////////////////////////
    }
  }
})();

这篇关于Dojo - 问题加载窗口小部件跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:47