ext js 有条件地将侦听器应用于 componenet

ext js apply listener to componenet conditionally

本文关键字:侦听器 应用于 componenet js 有条件 ext      更新时间:2023-09-26

我得到了一个EXT JS应用程序,其中包含一个函数内部的datefield(该函数还呈现其他组件(。目前,该函数被调用两次以创建(从功能角度(两个相同的datefield组件。两个日期字段组件的区别在于 ID 和名称不同,因为它们的命名取决于传递给函数的参数。

我想为两个datefield组件提供略有不同的行为,而无需创建两个不同的datefield组件。我想知道是否只有在满足某些条件的情况下才能将侦听器应用于组件?

例如:

if(some condition is true){
    select: function(datefield, date){
               //code
        }
}

或者我是否必须为两个组件提供所有侦听器,但在内部执行条件,以便如果不满足条件,则永远不会为该组件触发该事件。

例如:

select: function(datefield, date){
    if(some condition is true){
        //code             
    }
    else{
       //Do nothing as this component doesn't need this listener.
}
}

我相当确定第二种方法会起作用,但我不确定这是一个好主意。第一种方式似乎稍微干净一些;我不确定它看起来太好了,周围躺着空荡荡的尸体。

您可以使用

日期字段的on方法。

if(some condition){
    datefield.on('select', function(field,value){
        // code for your select here.
    });
}

我认为这应该满足您的需求。

我已经在其他组件上做了类似的实现,基本上是这样的:

 var yourComponent = Ext.create('...',{});
 if (something is true){
    yourComponent.on('select', this._doStuff, yourComponent);  
 }
//
 _doStuff: function(component){
   //do stuff
 }

希望这有帮助。