我在页面中放置Dojo小部件时遇到问题

时间:2013-01-12 01:44:02

标签: javascript dojo widget

我有一个简单的小部件:

define(["dojo/_base/declare", "dijit/_WidgetBase", "dojo/dom-construct"],
function(declare, WidgetBase, domConstruct){

    return declare("gijit.workflow.debug.combi", [WidgetBase], {
        startup: function(){
            alert("started");
        },
        buildRendering: function(){

            var container = domConstruct.create("div", {innerHTML:"test"}, this.domNode, "first");
            var rad1 = domConstruct.create("input", {type:"radio"}, container, "first");
            var rad1 = domConstruct.create("input", {type:"radio"}, container, "last");
        }
    });
});

和一个简单的加载页面:

<!DOCTYPE html>
<html >
<head>

<link rel="stylesheet" href="../../../css/tcs-style-dijit.css" />

<script>dojoConfig = {parseOnLoad: false}</script>
<script src="../../../js/dojo/dojo.js" data-dojo-config="async: true"></script>
<!-- <script src="../../../js/gijit/workflow/debug/combi.js"></script> -->
<script>
require(["dojo/dom", "gijit/workflow/debug/combi", "dojo/parser",
        "dojo/_base/window"], function(dom, combi, parser, window) {
    var widget = new combi();
    widget.placeAt(window.body());
//  widget.startup();
});
</script>
</head>
<body id="dd" class="tcs">
</body>
</html>

但是,我收到以下错误:

组件返回失败代码:0x80004003(NS_ERROR_INVALID_POINTER)[nsIDOMHTMLBodyElement.appendChild] 域“&gt; http:// 端口 /js/dojo/dojo.js 第15行

我已将错误隔离到我尝试将小部件放在HTML中的位置:

widget.placeAt(window.body());

我无法弄清楚导致问题的原因,并希望得到一些帮助

1 个答案:

答案 0 :(得分:1)

在您调用window.body()时,DOM尚未被解析,因此返回undefined。需要dojo/domReady!插件作为最后一个模块加载:

require([
    "dojo/dom",
    "gijit/workflow/debug/combi",
    "dojo/parser",
    "dojo/_base/window",
    "dojo/domReady!"  // ==> wait for the DOM to be ready
], function(dom, combi, parser, win) {
    var widget = new combi();
    widget.placeAt(win.body());
    widget.startup();
});

请注意,作为最佳做法,我建议不要通过将window模块分配到本地dojo/_base/window变量来隐藏window对象,而是使用win。< / p>