【问题标题】:Cannot seem to respond to event fire in sencha touch似乎无法响应煎茶触摸中的事件触发
【发布时间】:2012-01-25 19:18:36
【问题描述】:

我正在尝试在 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);
                    }
                }
            },

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

【问题讨论】:

  • 试试console.log(Ext.getCmp("usersForm"))看看会出现什么

标签: javascript model-view-controller sencha-touch extjs


【解决方案1】:

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

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

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

【讨论】:

  • 在我正在监听的同一元素上触发事件修复了这个问题。谢谢!我的假设是你可以像在 Mootools 中那样设置通用监听器,让它们监听事件触发,然后适当地执行它们的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 2012-11-04
  • 1970-01-01
相关资源
最近更新 更多