防止Ext.tab.Panel上的标签更改?

时间:2013-08-05 15:54:21

标签: sencha-touch sencha-touch-2

我有一个标签,用户必须输入信息,我想在用户尝试更改标签时验证数据。如果数据输入不完整,我会提示用户验证他是否要离开。

我在选项卡面板上添加了监听器 activeitemchange:,我可以提示用户。但是,似乎Ext.Msg不阻止方法调用,我总是从activeitemchange“返回true”。

如何更改代码,以便仅在用户点击“是”时更改标签?

Ext.Loader.setConfig({enabled: true});

Ext.application({
    name: "Sencha",

    launch: function () {

        var tabPanel = Ext.create('Ext.tab.Panel', {
            layout: 'card',
            tabBarPosition: 'bottom',
            listeners: {

                activeitemchange: function (me, value, oldValue, eOpts) {
                    console.log("activeitemchange");

                    var oldTabIdx = me.getInnerItems().indexOf(oldValue);
                    var newTabIdx = me.getInnerItems().indexOf(me.getActiveItem());

                    console.log(oldTabIdx + " => " + newTabIdx);
                    if (oldTabIdx == 2) {
//                  me.getTabBar().setActiveItem(2);
                        Ext.Msg.confirm("Leave Screen?", "Are you sure you?", function (response) {
                            console.log(response);
                            if (response === 'no') {
                                console.log("User says stay.");
                                return false;
                            } else {
                                console.log("User says leave.");
                                return true;
                            }
                        });
                    }
                    // Always returns true because Ext.Msg.confirm doesn't block
                    return true; // false prevents tab change
                }
            },
            items: [
                {
                    id: 'tab1',
                    title: 'Home',
                    layout: 'fit',
                    xtype: 'container',
                    iconCls: 'home',
                    items: [
                        {html: 'page #1'}
                    ]
                },
                {
                    id: 'tab2',
                    title: 'Two',
                    layout: 'fit',
                    xtype: 'container',
                    iconCls: 'star',
                    items: [
                        {html: 'page #2'}
                    ]
                },
                {
                    id: 'tab3',
                    title: 'three',
                    layout: 'fit',
                    xtype: 'container',
                    iconCls: 'team',
                    items: [
                        {html: 'page #3'}
                    ]
                }
            ]
        });
        Ext.Viewport.add(tabPanel);
    }
});

尝试修复:

Ext.Loader.setConfig({enabled: true});

Ext.application({
    name: "Sencha",

    launch: function () {

        var tabPanel = Ext.create('Ext.tab.Panel', {
            layout: 'card',
            tabBarPosition: 'bottom',
            confirm: true,

            listeners: {

                activeitemchange: {
                    order: 'before',

                    fn: function (list, value, oldValue, eOpts) {
                        var me = this;

                        console.log(me.getConfirm()); // ERROR: getConfirm() is undefined

                        var oldTabIdx = me.getInnerItems().indexOf(oldValue);
                        var newTabIdx = me.getInnerItems().indexOf(value);

                        console.log(oldTabIdx + " => " + newTabIdx);

                        if (oldTabIdx == 2 && me.getConfirm()) {
                            Ext.Msg.confirm("Leave Screen?", "Are you sure you?", function (response) {
                                console.log(response);
                                if (response === 'no') {
                                    console.log("User says stay.");
                                } else {
                                    console.log("User says leave.");
                                    me.setConfirm(false);
                                    me.setActiveItem(newTabIdx);
                                }
                            });
                            return false;
                        } else {
                            me.setConfirm(true);
                        }
                    }

                }
            },

            items: [
                {
                    id: 'tab1',
                    title: 'Home',
                    layout: 'fit',
                    xtype: 'container',
                    iconCls: 'home',
                    items: [
                        {html: 'page #1'}
                    ]
                },
                {
                    id: 'tab2',
                    title: 'Two',
                    layout: 'fit',
                    xtype: 'container',
                    iconCls: 'star',
                    items: [
                        {html: 'page #2'}
                    ]
                },
                {
                    id: 'tab3',
                    title: 'three',
                    layout: 'fit',
                    xtype: 'container',
                    iconCls: 'team',
                    items: [
                        {html: 'page #3'}
                    ]
                }
            ]
        });
        Ext.Viewport.add(tabPanel);
    }
});

1 个答案:

答案 0 :(得分:3)

问题是因为Ext.Msg.confirm()是一个异步方法。它会打开一个不同的线程,因此无论在子线程上发生什么情况,侦听器都会继续正常执行。

我看到的唯一方法是在'yes'回调中触发setActiveItem()。

另外,为避免确认循环,我添加了一个标志:

Ext.application({
    name: "Sencha",

    requires: [
        'Sencha.MyPanel',
        'Ext.MessageBox'
    ],

    launch: function () {
        var tabPanel = Ext.create('Sencha.MyPanel');
        Ext.Viewport.add(tabPanel);
    }
});

Ext.define('Sencha.MyPanel', {
    extend: 'Ext.tab.Panel',

    config: {
        layout: 'card',
        tabBarPosition: 'bottom',
        confirm: true,

        listeners: {
            activeitemchange: {
                order: 'before',

                fn: function (list, value, oldValue, eOpts) {
                    var me = this;

                    console.log(me.getConfirm()); // ERROR: getConfirm() is undefined

                    var oldTabIdx = me.getInnerItems().indexOf(oldValue);
                    var newTabIdx = me.getInnerItems().indexOf(value);

                    console.log(oldTabIdx + " => " + newTabIdx);

                    if (oldTabIdx == 2 && me.getConfirm()) {
                        Ext.Msg.confirm("Leave Screen?", "Are you sure you?", function (response) {
                            console.log(response);
                            if (response === 'no') {
                                console.log("User says stay.");
                            } else {
                                console.log("User says leave.");
                                me.setConfirm(false);
                                me.setActiveItem(newTabIdx);
                            }
                        });
                        return false;
                    } else {
                        me.setConfirm(true);
                    }
                }
            }
        },

        items: [
            {
                id: 'tab1',
                title: 'Home',
                layout: 'fit',
                xtype: 'container',
                iconCls: 'home',
                items: [
                    {html: 'page #1'}
                ]
            }, {
                id: 'tab2',
                title: 'Two',
                layout: 'fit',
                xtype: 'container',
                iconCls: 'star',
                items: [
                {html: 'page #2'}
                ]
            }, {
                id: 'tab3',
                title: 'three',
                layout: 'fit',
                xtype: 'container',
                iconCls: 'team',
                items: [
                    {html: 'page #3'}
                ]
            }
        ]
    }
});

希望有所帮助 -