Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Accessing controller from button handler

  1. #1
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    4
      0  

    Default 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 - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3932
    Vote Rating
    1272
      0  

    Default

    Add a listener for the button in the controller
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  3. #3
    Sencha User rdougan's Avatar
    Join Date
    Oct 2008
    Posts
    1,159
    Answers
    93
    Vote Rating
    7
      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
    4
      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
    4
      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 User rdougan's Avatar
    Join Date
    Oct 2008
    Posts
    1,159
    Answers
    93
    Vote Rating
    7
      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
    4
      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 User rdougan's Avatar
    Join Date
    Oct 2008
    Posts
    1,159
    Answers
    93
    Vote Rating
    7
      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
    4
      0  

    Default

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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •