【问题标题】:What is correct way to pass on event parameter in DOJO?在 DOJO 中传递事件参数的正确方法是什么?
【发布时间】:2017-10-08 03:11:07
【问题描述】:

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

<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"
		 />

【问题讨论】:

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


    【解决方案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);
    });
    

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

    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'

    希望这会有所帮助。

    【讨论】:

    • 谢谢。这就是我要找的。而我对那额外的 r 不利
    【解决方案2】:

    您应该能够通过以下方式将 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 2016-06-07
      相关资源
      最近更新 更多