Results 1 to 8 of 8

Thread: never, ever call a controller method from a view

  1. #1
    Sencha Premium Member
    Join Date
    Mar 2011
    Location
    Betelgeuse VII
    Posts
    100
    Answers
    4

    Default Answered: never, ever call a controller method from a view

    Apparently this went from *how* (you can) to *should* (no you shouldn't).

  2. In your case, myapp is just a namespace where all your application reside. You should assign your application object to this namespace before using it globally. This can be done in the launch() method.
    Code:
    Ext.application({
        name: 'myapp',
        ...
        launch: function(){
            ...
            myapp.application = this;
        }
    });
    Then you can refer to a controller from any point in your application with this code
    Code:
    myapp.application.getController('Home');

  3. #2
    Sencha User
    Join Date
    Jan 2012
    Posts
    1,376
    Answers
    347

    Default

    In your case, myapp is just a namespace where all your application reside. You should assign your application object to this namespace before using it globally. This can be done in the launch() method.
    Code:
    Ext.application({
        name: 'myapp',
        ...
        launch: function(){
            ...
            myapp.application = this;
        }
    });
    Then you can refer to a controller from any point in your application with this code
    Code:
    myapp.application.getController('Home');

  4. #3
    Sencha Premium Member
    Join Date
    Mar 2011
    Location
    Betelgeuse VII
    Posts
    100
    Answers
    4

    Default

    Thanks

  5. #4
    Sencha User
    Join Date
    Feb 2010
    Posts
    125
    Answers
    7

    Default

    maybe you should consider to not call the controller method from a view, because there was a reason that you can't access it directly

    if you want to communicate with your controller use events...

  6. #5
    Sencha User castitas's Avatar
    Join Date
    Sep 2011
    Location
    US
    Posts
    116
    Answers
    3

    Default

    The docs have good examples of using events in the controller (see the second code box). It really is easier (and best practice).
    word

  7. #6
    Sencha Premium Member
    Join Date
    Mar 2011
    Location
    Betelgeuse VII
    Posts
    100
    Answers
    4

    Default

    Very true. It is easier and it is best practice.

  8. #7
    Sencha User jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    DC Area =)
    Posts
    16,364
    Answers
    102

    Default

    Quote Originally Posted by brn View Post
    maybe you should consider to not call the controller method from a view, because there was a reason that you can't access it directly

    if you want to communicate with your controller use events...
    brn is absolutely correct. There seems to be a growing sense of bad practice around where people are wiring up views to have references to their controllers. This is not recommended.

  9. #8
    Sencha Premium Member
    Join Date
    Mar 2011
    Location
    Betelgeuse VII
    Posts
    100
    Answers
    4

    Default

    Calling a controller from a view has always been and will always be bad practice...

Tags for this Thread

Posting Permissions

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