Sencha Touch 2:如何在类定义中引用self

时间:2013-01-29 16:20:59

标签: extjs extjs4 sencha-touch sencha-touch-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();
                                        }
                                    }
                                ]
                            });
                        }
                    }
                ]
            }
        ]
    }
});

2 个答案:

答案 0 :(得分:3)

this中使用handler时,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);
}

Check out this working example.

答案 1 :(得分:3)

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

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

在此处阅读更多内容:http://docs.sencha.com/touch/2-1/#!/api/Ext.Button-method-up