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

时间:2017-10-08 03:11:07

标签: javascript dojo custom-widgets ibm-content-navigator

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

<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 总是以未定义的方式出现。我对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"
		 />

2 个答案:

答案 0 :(得分:1)

首先,“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);
});

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

一样完成
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'

希望这有帮助。

答案 1 :(得分:0)

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

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

示例:

<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();