0

所以我的主 app.js 中有 #add_button:

{ xtype: 'button', text: 'Add', itemId: 'add_criteria' }

我在这里有一个控制器,它监听每次点击,并在每次点击 #add_button 时尝试添加 1:

Ext.define('AM.controller.Add', {
    extend: 'Ext.app.Controller',
    init: function() {
        this.control({
            '#add_button': {
                click: this.add
            }
        });
    },

    add: function(btn) {
        var count = 0;
        if (count <= 3)
        {
            count++;
            console.log('Count is now ' + count;

        }
        else {
            console.log('wut');
        }

    }
});

控制器设置正确,但我似乎无法计算点击次数。它告诉我它是“未定义的”。有任何想法吗?

是的,我在“按钮”组件上看到了 Sencha 文档。但是,我正在使用控制器处理事件。

4

1 回答 1

1

您将count其用作局部变量,并在每次单击按钮时将其初始化为 0。您需要创建count控制器的成员变量。

Ext.define('AM.controller.Add', {
    extend: 'Ext.app.Controller',
    init: function() {
        this.count = 0;
        this.control({
            '#add_button': {
                click: this.add
            }
        });
    },

    add: function(btn) {
        if (this.count <= 3)
        {
            this.count++;
            console.log('Count is now ' + this.count);

        }
        else {
            console.log('wut');
        }

    }
});
于 2013-08-01T14:09:04.900 回答