似乎无法对sencha触摸中的事件火做出响应

Cannot seem to respond to event fire in sencha touch

本文关键字:事件 响应 触摸 sencha      更新时间:2023-09-26

我试图在sencha touch中触发一个自定义事件,它似乎被添加到了可以触发的事件中,并且似乎被触发了,但我的侦听器似乎没有响应该事件。

事件在这里被实例化,这里是表单体:

Ext.apply(this, {
        scroll: 'vertical',
        dockedItems: [ titlebar, buttonbar ],
        items: [ fields ],
        listeners: {
            beforeactivate: function() {
                var saveButton = this.down('#userFormSaveButton');
                    saveButton.setText('Submit');
                    this.addEvents('cityUpdate');
                    console.log('event added');
            },
            deactivate: function() {
                this.resetForm();
            }
        }
    });

然后在此处触发事件:

  if (result.data != 'false' && result.data.state) {
        Ext.getCmp("usersForm").fireEvent('cityUpdate', result.data.towns);
        console.log('event fired');
        alert('Your town is ' + result.data.towns + ' ' + result.data.state.replace( /'+/g , ' ' ));
    }

然后侦听器应用于以下形式的字段:

                {
                xtype: 'textfield',
                name: 'city',
                label: 'City*',
                placeHolder: 'New York',
                useClearIcon: true,
                id:'city',
                listeners: {
                    cityUpdate: function(city) {
                        console.log('event reponse' , this.fieldEl.dom , city);
                    }
                }
            },

有人能向我解释为什么我无法在控制台中看到事件响应调用吗?我看到添加了事件并触发了事件,但我似乎从未让事件真正运行事件响应的控制台语句。我在控制台中没有收到任何错误,所以我有点不知道这里发生了什么?

看起来表单正在触发事件,但您已将侦听器添加到表单上的文本字段中。

如果usersForm是调用fireEvent的,那么该事件的侦听器需要位于usersForm本身上。

如果在与beforeactivatedeactivate相同的块中,将cityUpdate处理程序添加到第一段代码中,会发生什么?这能抓住事件吗?如果是这样,你没想到它会在那里被抓住吗?如果是这样的话,可能是一个轻微的设计问题。