1. #1
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    2
    cyberwombat is on a distinguished road

      0  

    Default 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.

    Code:
       
    xtype: 'button',handler: function() {
    main = this.getMain();  //doesn't work - 'main' is a ref in my controller.
    thanks

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,522
    Answers
    3447
    Vote Rating
    814
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Add a listener for the button in the controller
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha - Sencha Touch Dev Team rdougan's Avatar
    Join Date
    Oct 2008
    Posts
    1,158
    Answers
    93
    Vote Rating
    6
    rdougan will become famous soon enough

      0  

    Default


    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:

    Code:
    init: function() {
        this.control({
            'button': {
                tap: function() {
                    console.log('Do something!');
                }
            }
        });
    }
    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.

  4. #4
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    2
    cyberwombat is on a distinguished road

      0  

    Default


    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. #5
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    2
    cyberwombat is on a distinguished road

      0  

    Default


    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

  6. #6
    Sencha - Sencha Touch Dev Team rdougan's Avatar
    Join Date
    Oct 2008
    Posts
    1,158
    Answers
    93
    Vote Rating
    6
    rdougan will become famous soon enough

      0  

    Default


    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!'
    }

  7. #7
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    2
    cyberwombat is on a distinguished road

      0  

    Default


    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

  8. #8

  9. #9
    Sencha - Sencha Touch Dev Team rdougan's Avatar
    Join Date
    Oct 2008
    Posts
    1,158
    Answers
    93
    Vote Rating
    6
    rdougan will become famous soon enough

      0  

    Default


    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?

  10. #10
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    2
    cyberwombat is on a distinguished road

      0  

    Default


    Ok I got it to work in my test case. Not sure what is up with my app... Thank you