Sencha Touch 2:如何覆盖导航视图上的后退按钮

时间:2013-02-13 17:16:03

标签: javascript mobile extjs sencha-touch sencha-touch-2

我想知道如何覆盖导航视图上的后退按钮。我尝试使用onBackButtonTap,但它似乎不起作用http://www.senchafiddle.com/#8zaXf

var view = Ext.Viewport.add({
            xtype: 'navigationview',
            onBackButtonTap: function () {
                alert('Back Button Pressed');
            },

            //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
                    view.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
                    view.pop();
                }
                }
            ]
        });

1 个答案:

答案 0 :(得分:5)

你提到的小提琴在我机器上的本地项目中运作良好。出于某种原因,它并不适用于小提琴网站。尝试在本地项目上运行它。

仍然不是使用onBackButtonTap配置,而是扩展Ext.navigation.View类并覆盖onBackButtonTap方法。这样您就可以更好地控制整个组件。您也想覆盖其他配置。这是我使用的 -

Ext.namespace('Ext.ux.so');

Ext.define('Ext.ux.so.CustomNav',{
    extend:'Ext.navigation.View',
    xtype:'customnav',
    config:{

    },
    onBackButtonTap:function(){
        this.callParent(arguments); 
        alert('back button pressed');
    }
});

this.callParent(arguments)将允许组件以默认方式运行+希望它的行为方式。如果要完全覆盖后退按钮行为,可以删除此行。尝试两种方式。

要使用此自定义组件,您可以使用 -

launch: function() {
        // Destroy the #appLoadingIndicator element
        Ext.fly('appLoadingIndicator').destroy();


        var view = Ext.create('Ext.ux.so.CustomNav', {
            fullscreen: true,

            items: [{
                title: 'First',
                items: [{
                    xtype: 'button',
                    text: 'Push a new view!',
                    handler: function() {
                        //use the push() method to push another view. It works much like
                        //add() or setActiveItem(). it accepts a view instance, or you can give it
                        //a view config.
                        view.push({
                            title: 'Second',
                            html: 'Second view!'
                        });
                    }
                }]
            }]
        });

    }

给它一个机会。它会为你工作。