在 DOJO 中传递事件参数的正确方法是什么?

What is correct way to pass on event parameter in DOJO?

我正在开发 Dojo 版本 1。8.I 设计了一个自定义小部件,如下所示。这是一个片段

<div>
 <div>
   <input 
    id ="NZ1",
    data-dojo-attch-point = "NZ1"
    data-dojo-attch-type = "ecm.widget.ValidationTextBox"
    data-dojo-attch-event = "onBlur : makeAllSmall"
   />
 </div>
 <div>
   <input 
    id ="NZ2",
    data-dojo-attch-point = "NZ2"
    data-dojo-attch-type = "ecm.widget.ValidationTextBox"
    data-dojo-attch-event = "onBlur: makeAllSmall"
   />
 </div>
</div> 

这是事件处理程序

makeAllSmall : function(evt){  
 var currVal=evt.target.value;
 currVal = currVal.toLowerCase();
 /**Some Other business logic on currVal **/
}

这个 evt 总是以 undefined 的形式出现。我对 Dojo 很陌生。我在 HTML 方面遗漏了什么吗?我尝试如下更改 HTML 但不是运气

   <input 
    id ="NZ2",
    data-dojo-attch-point = "NZ2"
    data-dojo-attch-type = "ecm.widget.ValidationTextBox"
    data-dojo-attch-event = "onBlur : makeAllSmall"
    data-dojo-args="e"
   />

首先,方法中是否有错字"onBlurr"?我看到有一个额外的 'r'。不应该是"onBlur"吗?

如果您查看 onBlur 事件的 DOJO API 文档,它不会像您期望的那样传递事件对象

onBlur()
Defined by: dijit/_FocusMixin

Called when the widget stops being "active" because focus moved to something outside of it, or the user clicked somewhere outside of it, or the widget was hidden.
Examples
Example 1
var btn = new Button();
// when /my/topic is published, this button changes its label to
// be the parameter of the topic.
btn.subscribe("/my/topic", function(v){
this.set("label", v);
});

接下来,在您的事件处理程序中,您尝试将文本更改为小写,这可以像

一样完成
makeAllSmall : function(){  
    var currVal=this.get("value");
    currVal = currVal.toLowerCase();
    /**Some Other business logic on currVal **/
}

没有事件处理程序的另一种方法是强制 ValidationTextBox 使用构造参数将所有内容转换为小写,例如

<input 
             id ="NZ2",
             data-dojo-attach-point = "NZ2"
             data-dojo-attach-type = "ecm.widget.ValidationTextBox"
            data-dojo-props='lowercase:true'
             data-dojo-attach-event = "onBlurr : makeAllSmall"
         />

注意我已经添加了data-dojo-props='lowercase:true'

希望这对您有所帮助。

您应该能够通过以下方式将 DOM 事件附加到您的自定义小部件:

  • 在标记中使用数据属性 data-dojo-attach-event
  • 并使用 _AttachMixin 传递您的回调函数。

示例:


<div id="somenode"><span data-dojo-attach-point="anattachpoint"
     data-dojo-attach-event="click: clicked">Click me</span></div>

var MyDijit = declare([ _WidgetBase, _AttachMixin ], {
    // .. declaration goes here ..
    clicked: function(e) {
        // handle event
    }
});
// instantiate the dijit instance, which will attach to the 'somenode' div.
var mydijit = new MyDijit({}, dom.byId('somenode'));
mydijit.startup();