如何在DOJO中将Widget添加到另一个Widget?

时间:2015-03-17 10:47:50

标签: javascript dojo

当用户点击一个按钮时,我需要向WidgetA添加一个WidgetB(我想使用WidgetA容器节点)。

我在按钮事件监听器中尝试了以下但没有成功:

   var item = registry.byId('wA');
   item.containerNode.innerHTML = '<span id="wB" data-dojo-type="WidgetB"></span>'
   parser.instantiate(['wB']);

我需要一个有效的例子,谢谢。

http://jsbin.com/johixagebo/1/


<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.3/dojo/dojo.js"></script>
    <script>
        require([
            "dojo/_base/declare", "dojo/parser", "dojo/ready",
            "dijit/_WidgetBase", "dijit/_TemplatedMixin", "dojo/_base/lang", "dijit/registry"
        ], function (declare, parser, ready, _WidgetBase, _TemplatedMixin, lang, registry) {
            var propA = {
                templateString: "<div>" +
                    "<div>WidgetA: <span data-dojo-attach-point='customNode'></span></div>" +
                    '<div data-dojo-attach-point="containerNode"></div>' +
                    "</div>",
                custom: "CUSTOM DEFAULT",
                _setCustomAttr: function (value) {
                    this.custom = value;
                    this.customNode.innerHTML = value;
                }
            };
            var propB = {
                templateString: "<div>" +
                    "<div>WidgetB: <span data-dojo-attach-point='customNode'></span></div>" +
                    "</div>",
                custom: "CUSTOM DEFAULT",
                _setCustomAttr: function (value) {
                    this.custom = value;
                    this.customNode.innerHTML = value;
                }
            };
            declare("WidgetA", [_WidgetBase, _TemplatedMixin], propA);
            //declare("WidgetB", [_WidgetBase, _TemplatedMixin], propA);


            ready(function () {
                parser.parse();
                registry.byId("wA").set("custom", 'XYZ');
                document.getElementById('btn').addEventListener('click', function () {
                    var item = registry.byId('wA');
                    item.containerNode.innerHTML = '<span id="wB" data-dojo-type="WidgetB"></span>'
                    parser.instantiate(['wB']);
                }.bind(this));
            });
        });
    </script>

</head>

<body>
    <span id="wA" data-dojo-type="WidgetA"></span>
    <input type="button" id="btn" value="add child B to A">
</body>

</html>

1 个答案:

答案 0 :(得分:0)

我能够使用以下代码找到解决方案:

使用:

基本上我使用包含containerNode数据属性的新HTML将DOM元素dojo-type修改为我的自定义窗口小部件(WigetB)。

在我允许使用parse.initiate()或parser.parser()基于新添加的Widget选择性地进行DOJO解析之后。

我仍然非常有兴趣知道是否有更好的方法来实现相同的结果,因为我不确定选择性解析是否是最佳选择。

注意:

parser.parser() 如果HTML包含几个嵌套级别的小部件,也可以很好地工作。 parser.initiate() 仅适用于1级嵌套。


<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.3/dojo/dojo.js"></script>
    <style>
        #wA {
            top: 0;
            background-color: lightblue;
        }

        #wB {
            top: 100px;
            background-color: lightcoral;
        }
    </style>
    <script>
        require([
            "dojo/_base/declare", "dojo/parser", "dojo/ready",
            "dijit/_WidgetBase", "dijit/_TemplatedMixin", "dojo/_base/lang", "dijit/registry"
        ], function (declare, parser, ready, _WidgetBase, _TemplatedMixin, lang, registry) {
            var propA = {
                templateString: "<div>" +
                    "<div><span data-dojo-attach-point='customNode'></span></div>" +
                    '<div data-dojo-attach-point="containerNode"></div>' +
                    "</div>",
                custom: "CUSTOM DEFAULT",
                _setCustomAttr: function (value) {
                    this.custom = value;
                    this.customNode.innerHTML = value;
                }
            };
            var propB = {
                templateString: "<div>" +
                    "<div><span data-dojo-attach-point='customNode'></span></div>" +
                    "</div>",
                custom: "CUSTOM DEFAULT",
                _setCustomAttr: function (value) {
                    this.custom = value;
                    this.customNode.innerHTML = value;
                }
            };
            declare("WidgetA", [_WidgetBase, _TemplatedMixin], propA);
            declare("WidgetB", [_WidgetBase, _TemplatedMixin], propA);


            ready(function () {
                parser.parse();
                registry.byId("wA").set("custom", 'XYZ');
                document.getElementById('btn').addEventListener('click', function () {
                    var item = registry.byId('wA');
                    item.containerNode.innerHTML = '<span id="wB" data-dojo-type="WidgetB"></span>'
                    parser.instantiate([dojo.byId("wB")]);

                    console.log(registry.byId('wA'));
                    console.log(registry.byId('wB'));

                }.bind(this));
            });
        });
    </script>

</head>

<body>
    <span id="wA" data-dojo-type="WidgetA"></span>
    <input type="button" id="btn" value="add child B to A">
</body>

</html>