从ExtJS中的组件中删除侦听器

Oli*_*ins 1 extjs

我的两个文本字段之间发生了双向更新(注意:我不想为此使用MVVM绑定).

当按键时,textfield1更新textfield2,反之亦然.我需要在接收文本字段上禁用侦听器,否则我最终会进入无限循环.

Ext.define('MyApp.view.TestView', {
    extend: 'Ext.panel.Panel',
    layout: 'form',

    items: [{
        id: 'tf1',
        xtype: 'textfield',
        fieldLabel: '1',
        listeners: {
            change: function() {
                var r = Ext.ComponentQuery.query('#tf2');
                r[0].setValue(this.getValue() + "!")
            }
        }
    }, {
        id: 'tf2',
        xtype: 'textfield',
        fieldLabel: '2',
        listeners: {
            change: function() {
                var r = Ext.ComponentQuery.query('#tf1');
                r[0].setValue(this.getValue() + "!")
            }
        }

    }]
});
Run Code Online (Sandbox Code Playgroud)

显然在上面的例子中,如果我做了一个按键,我最终会得到两个带有无限'!'的文本字段.在末尾.

小提琴在这里:

https://fiddle.sencha.com/#fiddle/1ces

我需要禁用监听器.我看了' removeListener ',我想你会像这样使用它:

        r[0].removeListener('change');
Run Code Online (Sandbox Code Playgroud)

但是我需要一个对我的监听器的引用,所以我可以在setValue(..)发生后添加它.

Oli*_*ins 5

经过深入挖掘后,我发现suspendEventsresumeEvents解决了我的问题.

        r[0].suspendEvents();

        r[0].setValue(this.getValue() + "!")

        r[0].resumeEvents();
Run Code Online (Sandbox Code Playgroud)

禁用所有侦听器并重新启用它们.

  • 请注意,您可以使用`suspendEvent`并传递名称来暂停特定事件. (3认同)