gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:31:19 25 4
gpt4 key购买 nike

当用户单击按钮时,我需要将 WidgetB 添加到 WidgetA (我想使用 WidgetA containerNode )。

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

   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>

最佳答案

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

使用:

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

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

我仍然很想知道是否有更好的方法来达到相同的结果,因为我不确定选择性解析是否是最佳选择。

注释:

解析器.解析器()如果 HTML 包含多个嵌套级别的小部件,也可以很好地工作。解析器.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>
<小时/>

关于javascript - 如何在 DOJO 中将一个 Widget 添加到另一个 Widget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29097126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com