dojo 小部件上未触发自定义事件

2024-04-17

我有 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)
        }
    });

});

//小部件.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() http://dojotoolkit.org/api/?qs=1.9/dijit/_WidgetBase#1_9dijit__WidgetBase_on您可以使用它,例如:

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

小部件扩展点

我不确定是什么emit()函数确实如此,但问题似乎就在这里。根据the docs http://dojotoolkit.org/reference-guide/1.9/quickstart/writingWidgets.html#creating-extension-points您应该只使用简单的函数来编写扩展点。例如:

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()有正确的论点。该函数供公共使用,可用于绑定自定义事件处理程序。

这里是完整的例子 http://jsfiddle.net/q5Gh7/.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

dojo 小部件上未触发自定义事件 的相关文章

随机推荐