Search code examples
extjssencha-touchsencha-touch-2

Sencha Touch 2 - How to check if an item exists in the ViewPort


What I want to do, basically, is to dynamically load views into the ViewPort when needed. In order to do that, I need to find out if the view was already added to the ViewPort so it doesn't get added multiple times. However looking at sencha touch documentation I was not able to find a proper way to check if an item exists inside the ViewPort.

So in summary

1 - Check if the ViewPort contains the view I want to set as active. Something like this:

if(!Ext.Viewport.items.contains('myviewtoactivate'))
    Ext.Viewport.add([{ xtype: 'myviewtoactivate' }]);

2 - Then I would set this view as active (this part I already know how to do).

Thanks for the help!


Solution

  • You can use something like this inside your controller:

        var main = this.getMain(),
            myview = main.down('myview');
    
        if(!myview){
            myview = main.add({
                xtype: 'myview'
            });
        }