-
5 Nov 2011 6:46 PM #1
Unanswered: Accessing controller from button handler
Unanswered: Accessing controller from button handler
I have a button in a view (it's a card from a card layout) and I want to access the parent card container from a button handler to tell it to switch view. I am unclear as to how to access the controller.
The controller has an xtype of 'main' and I made a ref for it - I am able to switch from the control: section but would like it done in the view handler instead.
thanksCode:xtype: 'button',handler: function() { main = this.getMain(); //doesn't work - 'main' is a ref in my controller.
-
5 Nov 2011 7:11 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 34,107
- Vote Rating
- 453
- Answers
- 3156
Add a listener for the button in the controller
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
5 Nov 2011 7:57 PM #3
As Mitchell says, you should add a listener inside your controller. You can do this inside the 'init' function of your controller, using the 'control' method:
I suggest you change the 'button' query to something more specific. Right now it will call that method on every button in your application. I suggest you specify and id on your button, and change query to '#myButtonId'. This is the quickest way.Code:init: function() { this.control({ 'button': { tap: function() { console.log('Do something!'); } } }); }
-
5 Nov 2011 8:04 PM #4
yes - I mentioned in my post that I already have that - I was wondering about doing it from the button handler to reduce clutter
-
5 Nov 2011 8:31 PM #5
How do you access it by ID? - i've noticed that the card layout shows duplicate items? like it button with the id I created is invisible and sencha adds another button instead with no id. So referencing by ID doesn't work. I had huge issues with this card/id thing in other cases - I was able to use .down() to get to it but in the case of the controller control: section I don't have that option. Any idea?
Meaning:
Code:control: '#buttonid': //doesn't work 'viewxtype > button': works but not very specific
-
5 Nov 2011 8:35 PM #6
If you follow the MVC way, you shouldn't have any logic in your views. As far as I'm aware, there is currently no way to access controllers from your views with 2.0.
As for accessing the button, you should be able to just give the button config a 'id' property, and use that. If it does not work, or it is creating duplicate buttons, it is bug. Please post a test case in the bug forums and we can take a look.
Code:{ xtype: 'button', id: 'myButton', text: 'Button!' }
-
5 Nov 2011 8:41 PM #7
Ya doens't work - I don't think it's a bug though - it's a really really annoying feature of the card layout. Any item created in a card layout seems to be replicated somehow - I don't understand why but it's true. You can't access any card panel or items in a card panel by id - I posted before on this in the forum and was told that it was not possible - to use down() instead. Problem is I can't use that in control:
See here http://www.sencha.com/forum/newreply...9260&noquote=1
-
5 Nov 2011 8:49 PM #8
-
5 Nov 2011 8:53 PM #9
Still makes no sense to me. You should be able to access any component with it's ID at any time, even within a card layout.
Could you post a simple test case to replicate this behavior?
-
5 Nov 2011 9:41 PM #10
Ok I got it to work in my test case. Not sure what is up with my app... Thank you


Reply With Quote