0

我有一个简单的小部件:

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] domain">http:// domain : port /js/dojo/dojo.js Line 15

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

widget.placeAt(window.body());

我无法弄清楚是什么导致了这个问题,希望能得到一些帮助

4

1 回答 1

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/windowwindowwin

于 2013-01-13T07:33:46.973 回答