有没有办法断开dojoAttachEvent添加的事件

时间:2011-11-12 08:08:40

标签: dojo

我有一个dojo小部件,它使用一个自定义库代码,在其模板中有这样的链接。

<a href="#" dojoAttachEvent="onclick:_goBack" dojoAttachPoint="backButton">Go Back</a>

我需要找到一种方法来将此事件与我的小部件断开连接。我知道如何断开事件的唯一方法是使用

dojo.disconnect(handle)

如果我使用 dojo,connect()连接了事件,我可以使用它。 但是使用 dojoAttachEvent 我没有事件句柄,因此无法断开它。

注意: 改变这个html对我来说不是一个选项,因为这是我正在使用的外部库 此外,我不是在寻找断开所有事件的解决方案。

CODE:

otherWidget.js:

dojo.provide("otherWidget");
dojo.declare("otherWidget", [], {
    templateString : dojo.cache("otherWidget","templates/otherWidget.html"),
    _goBack: function(){        
        this.destroyWidgetAndRedirect();
    },
    destroyWidgetAndRedirect: function(){       
        //Code to destory and redirect.
    },
});

otherWidget.html:

<div>
<a href="#" dojoAttachEvent="onclick:_goBack" dojoAttachPoint="backButton">Go Back</a>
<!-- Other Widget related code -->
...
</div>

myWidget.js:

dojo.provide("myWidget");
dojo.require("otherWidget");
dojo.declare("myWidget", [], {
    templateString : dojo.cache("myWidget","templates/myWidget.html"),
    this.otherWidget = new otherWidget({}, dojo.byId('otherWidgetContainer'));
});

myWidget.html:

<div>
<div id="otherWidgetContainer"></div>
<!-- My Widget related code -->
...
</div>

任何想法.. 谢谢。

1 个答案:

答案 0 :(得分:1)

扩展点可以直接在您的html或javascript中使用。假设您使用的小部件称为“my.custom.dojowidget”,并且它具有onClick扩展点。我将在这里显示声明方式,在你的HTML中。试试这个:

<div data-dojo-type="my.custom.widget">
    <script type="dojo/method" data-dojo-event="onClick" data-dojo-args"evt">
         dojo.stopEvent(evt);
         console.debug("did this work ?");
    </script>
</div>

现在这取决于扩展点的存在......如果你仍然无法做你想做的事,请发布你小部件代码的相关部分。

所以...根据您在编辑中发布的示例代码,我认为您应该执行以下操作:

<div data-dojo-type="otherWidget">
    <script type="dojo/method" data-dojo-event="destroyWidgetAndRedirect" data-dojo-args="evt">
        dojo.stopEvent(evt);
        // do whatever custom code you want here...
    </script>
</div>