自定义事件不会在dojo小部件上触发 [英] custom event is not triggered on dojo widget

查看:138
本文介绍了自定义事件不会在dojo小部件上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有dojo自定义窗口小部件。

I have dojo Custom widget.

我需要从自定义窗口小部件发出一个事件
这个代码,我添加了事件监听器

I need to emit a event from the custom widget, this the code where I have added the event listener

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        var dojoConfig = {
            async: true,
            parseOnLoad: true,
            isDebug : true,
            packages:[
                {   name:"custom",
                    location:"/Test/js"
                }
            ]
        };
    </script>
    <script src="//localhost:8080/dojo1.9.0/dojo/dojo.js"></script>
</head>
<body>
<script>
    require(["custom/MyWidget","dojo/on","dojo/dom-construct","dojo/_base/window","dojo/domReady!"],function(MyWidget,on,domconstruct,window){
        var mywidget = new MyWidget();
        mywidget.startup();
        domconstruct.place(mywidget.domNode,window.body(),"after");


        on(mywidget,"customevent",function(data){
            console.log( " received notification "+data );
        });
    });
</script>
</body>
</html>

和小部件如下

define([
    "dojo/_base/declare",
    "dijit/_WidgetBase",
    "dijit/_OnDijitClickMixin",
    "dijit/_TemplatedMixin",
    "dojo/text!./widget.html",
    "dojo/on",
    "dojo/_base/lang"
], function (declare, _WidgetBase, _OnDijitClickMixin, _TemplatedMixin, template,on,lang) {

    return declare([_WidgetBase, _OnDijitClickMixin, _TemplatedMixin], {
        templateString: template,
        //  your custom code goes here
        _onClick:function()
        {

        },

        postCreate : function ()
        {
            on(this.searchtextbox,"click",lang.hitch(this,"sendEvent"));

        },
        sendEvent : function(event)
        {
            console.log( "Click event in widget "+event );
            this.emit("customevent",event.x)
        }
    });

});

// Widget.html

//Widget.html

<div class="${baseClass}" data-dojo-attach-point="searchtextbox">
    <p>
        here your widget
    </p>
</div>

问题是行

this.emit(customevent,event.x)正在抛出错误

推荐答案

小部件事件



您不应该使用 dojo / on 将事件处理程序添加到窗口小部件中,仅适用于 DOM节点。如果你从 dijit / _WidgetBase (像你这样做)延伸,那么有一个方法叫做 on() 可以使用,例如:

Widget events

You should not use dojo/on to add event handlers to widgets, it's only for DOM nodes. If you extend from dijit/_WidgetBase (like you do), then there's a method called on() which you can use, for example:

myWidget.on("customevent", function(data) {
    console.log( " received notification "+data );
});






小部件扩展点



我不知道 emit()函数是什么,但似乎这是问题。根据文档,您应该编写您的扩展名点只是简单的功能。例如:


Widget extension points

I'm not sure what the emit() function does, but it seems that here's the problem. According to the docs you should write your extension points just with plain functions. For example:

postCreate : function () {
    on(this.searchtextbox,"click",lang.hitch(this, "_onClick"));
},
_onClick: function(event) {
    // This is where you put your code
    console.log( "Click event in widget "+event );
    this.onCustomEvent(event.x);
},
onCustomEvent: function() {
     // This can be left empty, it will be used as the extension point
}

在这种情况下,您将绑定click事件到 _onClick()函数,反过来调用 onCustomEvent()使用正确的参数。此功能供公众使用,可用于绑定自定义事件处理程序。

In this case you will bind the click event to the _onClick() function, which in turn calls onCustomEvent() with the correct arguments. This function is for public use and can be used to bind custom event handlers.

这里是完整示例

这篇关于自定义事件不会在dojo小部件上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆