-
24 Oct 2011 3:54 PM #1
Answered: Help on switching views with buttons with MVC
Answered: Help on switching views with buttons with MVC
I'd like to understand how to switch views from a button. It seems that I can do so from either the control: in controller or the button handler and would like to see both examples (unless they are the same).
Also - what are the pros and cons of each approach? and are there other as well. And in the control section I used the ID of the button but wonder if that is the best way.
Thanks!
Here's my controller:
and my first view:Code:Ext.define('MyApp.controller.Test', { extend: 'Ext.app.Controller', config: { }, refs: [ { ref: 'first', selector: '#first' }, { ref: 'second', selector: '#second' } ], views : [ 'TestMain', 'TestSecond' ], init: function() { this.getTestMainView().create(); this.control({ '#first': { tap: function() { //how do I go to second view here? } }, '#second': { tap: function() { } } }); } });
and second view:Code:Ext.define('MyApp.view.TestMain', { extend: 'Ext.Container', xtype: 'testmain', config: { fullscreen: true, layout: 'vbox', scrollable: true, items: [ { xtype: 'button', ui: 'normal', id: 'first', text: 'Go To Second Screen', handler: function() { //how do I go to second view here? } } ] } });
Code:Ext.define('MyApp.view.TestSecond', { extend: 'Ext.Container', xtype: 'testsecond', config: { fullscreen: true, layout: 'vbox', scrollable: true, items: [ { xtype: 'button', ui: 'normal', id: 'second', text: 'Go To First Screen', handler: function() { } } ] } });
-
Best Answer Posted by AndreaCammarata
Hi joseph.
First, I suggest you to set the xtype of your second view by writing
Then, always in your second view, you will define a button with an "action" config:Code:alias: 'widget.secondview'
Then, in your controller init function you have to control that button handler, simply writingCode:... { xtype: 'button', action: 'backtofirst', ui: 'back', text: 'First view' } ...
And finally you have to define your "backToFirst" function inside your controller, that will be able to create the SecondView widget and set it as the active one inside the MainView:Code:this.control({ 'mainview button[action=backtofirst]': { tap: this.backToFirst } });
Hope this helps.Code:backToFirst: function(){ this.getMain().setActiveItem(Ext.widget('secondview')); }
-
24 Oct 2011 9:22 PM #2
-
24 Oct 2011 9:41 PM #3
I got it to work by following the methods used in the kitchensink example
Create another view like this called Main
Code:Ext.define('MyApp.view.Main', { extend: 'Ext.Panel', id: 'mainView', requires: [ 'MyApp.view.TestMain', 'MyApp.view.TestSecond' ], config: { fullscreen: true, layout: { type: 'card', animation: { type: 'slide', direction: 'left', duration: 250 } }, items: [{ xtype: 'testmain' }, { xtype: 'testsecond' }] } });
Then update your controller
Code:Ext.define('MyApp.controller.Test', { extend: 'Ext.app.Controller', config: { }, refs: [ { ref: 'main', selector: 'mainview', autoCreate: true, xtype: 'mainview' }, { ref: 'first', selector: '#first' }, { ref: 'second', selector: '#second' } ], views : [ 'Main', 'TestMain', 'TestSecond' ], init: function() { this.getTestMainView().create(); this.control({ '#first': { tap: this.goToSecondView }, '#second': { tap: function() { } } }); }, goToSecondView: function() { mainView = this.getMain(); secondView= this.getSecond(); mainView.setActiveItem(secondView); }, });
-
24 Oct 2011 11:01 PM #4
Yes that makes sense to use the card now that I think about it for the slide transition. My question then would have to be rephrased without the transition then. How would I just switch controller actions/views? without transition? It seems like I have to access the main viewport and tell it which view to create/show but no idea how to do that and no example I could find made use of that kind of switch,
Thanks!
-
30 Oct 2011 9:38 PM #5
So I am playing with this suggestion but cannot actually get it to work. Doesn't switch views. Any code missing here?
-
30 Oct 2011 11:26 PM #6
I don't know if this helps.
but try in your views to replace xtype with alias.
Remember to add widget in front of the xtype name as below.
That works for me
PHP Code://xtype: 'testsecond'
alias: 'widget.testsecond'
-
7 Nov 2011 3:42 PM #7
Help on MVC with switching buttons.
Help on MVC with switching buttons.
I'm in the same situation. My example is very similiar to what suggested above. I was not able to swich to second 2 container after clicking the button. I viewed the object and everything there there but it did not change to 2nd panel for me.?
...
goToSecondView: function() {
mainView = this.getMain();
secondView= this.getSecond();
mainView.setActiveItem(secondView);
}
..
-
8 Nov 2011 1:00 AM #8
Hi joseph.
First, I suggest you to set the xtype of your second view by writing
Then, always in your second view, you will define a button with an "action" config:Code:alias: 'widget.secondview'
Then, in your controller init function you have to control that button handler, simply writingCode:... { xtype: 'button', action: 'backtofirst', ui: 'back', text: 'First view' } ...
And finally you have to define your "backToFirst" function inside your controller, that will be able to create the SecondView widget and set it as the active one inside the MainView:Code:this.control({ 'mainview button[action=backtofirst]': { tap: this.backToFirst } });
Hope this helps.Code:backToFirst: function(){ this.getMain().setActiveItem(Ext.widget('secondview')); }Sencha Inc
Andrea Cammarata, Solutions Engineer
CEO at SIMACS
@AndreaCammarata
www.andreacammarata.com
github: https://github.com/AndreaCammarata
-
10 Nov 2011 4:50 PM #9
Awesome!, the one thing I can't figure out is how to change the animation direction, seems like setActiveItem() doesn't support them in ST2, or maybe I'm missing something! I just want to change the direction of my default animation when I click the "Back" button in my app, any tips?
-
10 Nov 2011 4:56 PM #10
Sencha Inc
Andrea Cammarata, Solutions Engineer
CEO at SIMACS
@AndreaCammarata
www.andreacammarata.com
github: https://github.com/AndreaCammarata


Reply With Quote
