【发布时间】:2011-10-21 16:53:57
【问题描述】:
我正在自定义小部件内实例化一个 dijit 按钮。那一点都很好。在小部件代码中,我绑定了一个 onclick 事件处理程序,但是当我单击按钮时,事件会触发两次。第二个问题是它还将点击事件绑定到页面中与小部件无关的其他按钮。下面是我所拥有的简化版本。谁能告诉我为什么这样做。我花了过去几个小时试图修复它。
代码如下,但你也可以在这里看到它
这是实例化自定义小部件的 html 页面 https://github.com/screenm0nkey/dojo/blob/master/widgets/destroy-widget.html
这是自定义小部件 https://github.com/screenm0nkey/dojo/blob/master/js/tag/widgets/DestroyWidget/Widget.js
这是包含嵌套小部件的模板 https://github.com/screenm0nkey/dojo/blob/master/js/tag/widgets/DestroyWidget/templates/template.html
这是小部件模板中的 html;
<div style="border: solid 1px pink">
<h3>${name}</h3>
<div dojoType="dijit.form.Button" data-dojo-attach-point="removeBtn" class="removeBtn">
click me.
</div>
这是绑定处理程序的小部件内的 JavaScript;
define('tag/Widget', ['dojo', 'dojo/parser', 'dijit/_Widget', 'dijit/_TemplatedMixin'],
function(d, parser) {
return d.declare('tag.Widget', [dijit._Widget, dijit._TemplatedMixin], {
templateString : d.cache("tag", "/templates/template.html"),
widgetsInTemplate : true,
name : 'no name',
button : 'no button',
postCreate : function() {
parser.parse(this.domNode);
this.placeAt(d.byId('authorContainer'));
},
startup : function() {
dijit.registry.forEach(dojo.hitch(this, function(w) {
if (w.class === 'removeBtn') {
this.button = w;
return;
}
}))
this.button.connect('onclick', function(evt) {
console.log(evt.target);
});
},
}); });
这是控制台输出;
<input type="button" value="" class="dijitOffScreen" tabindex="-1" role="presentation" data-dojo-attach-point="valueNode">
<span class="dijitReset dijitInline dijitButtonText" id="dijit_form_Button_0_label" data-dojo-attach-point="containerNode">click me.</span>
【问题讨论】:
-
添加了fiddle
标签: dojo