Sencha Touch 2 MVC - how to switch views with button

江枫思渺然 提交于 2019-11-30 05:07:24

Instead of the handlers, you should use this.control in the controller:

this.control({
    '#first': {
        tap: function() {
            Ext.Viewport.add({
                xtype: 'testsecond'
            });
        }
    },

(you can drop the handlers: from the button in the view definition)

Also, you probably shouldn't hard-code ids in the first place.

The way I handle buttons like this is:

items: [
            {
                xtype: 'button',
                ui: 'normal',
                text: 'Go To First Screen',
                go: 'testsecond'
            },
       ...

Then in a base controller that applies to all views, I do something like:

this.control({
    'button[go]': {
        tap: function(btn) {
            Ext.Viewport.setActiveItem({
                xtype: btn.go
            });
        }
    },
Roberto

Excellent answer but beware that calling setActiveItem() with just an xtype will create new views instead of reusing the ones that are already created, I ended up using Ext.ComponentQuery to get the target first and then setActiveItem on that, here's the code:

this.control({
        'button[go]': {
                tap: function(btn) {
                    viewport = Ext.ComponentQuery.query('my-custom-viewport-xtype');
                    target = Ext.ComponentQuery.query(btn.go);
                    viewport[0].setActiveItem(target[0]);
                }
            }
    });

Also note that I use a custom viewport to add slide effects and such, but calling directly Ext.Viewport.setActiveItem() like the previous example will work as well.

First we need to check if that xtype is already created or not. Following code will do that

var xtypeToDisplay = Ext.ComponentQuery.query(btn.go)[0];

if (xtypeToDisplay == undefined)
{
    xtypeToDisplay= Ext.create('Class_Name_for_that_Xtype');
}

This worked for me:

handler: function() {
    //how do I go to second view here?

    Ext.Viewport.setActiveItem(Ext.create('MyApp.view.TestSecond')); 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!