煎茶触摸2:如何在类定义中引用自我



我正在定义一个类,但在下面的代码中尝试引用自身时遇到了麻烦。 你可以看到我正在尝试起诉推送和弹出操作,但例如 this.self.pop() 不是引用类的正确方法。 在类定义中引用它的正确方法是什么?

//create the navigation view and add it into the Ext.Viewport
Ext.define('myApp.view.Settings', {
    extend: 'Ext.navigation.View',
    id:'view',
    xtype: 'navigationview',
    config: {
        title: 'Settings',
        iconCls: 'settings',
        //we only give it one item by default, which will be the only item in the 'stack' when it loads
        items: [
            {
                //items can have titles
                title: 'Navigation View',
                padding: 10,
                //inside this first item we are going to add a button
                items: [
                    {
                        xtype: 'button',
                        text: 'Push another view!',
                        handler: function () {
                            //when someone taps this button, it will push another view into stack
                            this.self.push({
                                //this one also has a title
                                title: 'Second View',
                                padding: 10,
                                //once again, this view has one button
                                items: [
                                    {
                                        xtype: 'button',
                                        text: 'Pop this view!',
                                        handler: function () {
                                            //and when you press this button, it will pop the current view (this) out of the stack
                                            this.self.pop();
                                        }
                                    }
                                ]
                            });
                        }
                    }
                ]
            }
        ]
    }
});

handler中使用this时,this将引用按钮,而不是视图。如果要获取Ext.navigation.View组件,并利用推送和弹出方法,请使用 Ext.getCmp(id):

Ext.getCmp('NavView').push(newView); // Instead of this.self.push()
Ext.getCmp('NavView').pop();         // Instead of this.self.pop();

按钮处理程序可能如下所示

handler: function() {
    var self = Ext.getCmp('NavView');
    var button = Ext.create('Ext.Button', {
        text: 'Pop View',
        id: 'button2',
        handler: function() {
            self.pop();
        }
    });
    var newView = {
        title: 'New View',
        id: 'NewView',
        items: [button]
    };
    self.push(newView);
}

看看这个工作示例。

还可以使用 up 方法查找父容器。

this.up('navigationview').push(...);

在这里阅读更多:http://docs.sencha.com/touch/2-1/#!/api/Ext.Button-method-up。

最新更新