【问题标题】:custom event is not triggered on dojo widget自定义事件未在 dojo 小部件上触发
【发布时间】:2014-02-18 09:11:48
【问题描述】:

我有 dojo 自定义小部件。

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

<!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

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

问题是线

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

【问题讨论】:

    标签: javascript events dojo


    【解决方案1】:

    小部件事件

    您不应使用dojo/on 将事件处理程序添加到小部件,它仅适用于DOM 节点。如果您从dijit/_WidgetBase 扩展(像您一样),那么您可以使用一个名为on() 的方法,例如:

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

    小部件扩展点

    我不确定emit() 函数是做什么的,但似乎问题出在这里。根据the docs,您应该只使用普通函数编写扩展点。例如:

    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
    }
    

    在这种情况下,您将单击事件绑定到 _onClick() 函数,该函数又使用正确的参数调用 onCustomEvent()。此函数为公共使用,可用于绑定自定义事件处理程序。

    这里是full example

    【讨论】:

    • 我已将监听器代码更改为如下codemywidget.on("customevent",function(data){ console.log( " received notification "+data ); });code还是不行:(
    • 看来emit() 函数正在引发错误。我不确定该函数的作用,但在文档中他们使用了不同的方式来创建自定义事件。我用一个小方法更新了我的答案。
    【解决方案2】:

    emit 方法的第二个参数应该是一个对象,但我没有向它发送对象。

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

    【讨论】:

      猜你喜欢
      • 2014-09-05
      • 1970-01-01
      • 2019-09-09
      • 2012-11-04
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多