2

我正在定义一个类,但在下面的代码中尝试引用自身时遇到了麻烦。您可以看到我正在尝试起诉 push 和 pop 操作,但是 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();
                                        }
                                    }
                                ]
                            });
                        }
                    }
                ]
            }
        ]
    }
});
4

2 回答 2

3

在 中使用thishandlerthis将指的是按钮,而不是视图。如果要获取Ext.navigation.View组件,并使用 push 和 pop 方法,请使用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);
}

查看这个工作示例。

于 2013-01-29T17:39:01.063 回答
3

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

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

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

于 2013-01-31T21:37:17.447 回答