4

The controller that controls an event using the recommended this.control: construct :

Ext.define('Mb.controller.Sav_vpc', {
    extend: 'Ext.app.Controller',
    init: function() {
        console.log('controller.init called');
        this.control({
            '#storeMenu menuitem': {
                click: this.onStoreMenuClicked
            }
        });
    }
});

The function onStoreMenuClicked gets called twice, because the init method of the controller gets called twice, therefore it listens twice to the event.

But when is the controller.init() called ? And why is it called twice ?

Here is my application.launch function:

Ext.define('Mb.Application', {
    extend: 'Ext.app.Application',
    launch: function() {
        console.log('launching app');
        var controller = Mb.app.getController('Name');
            console.log('end launching app');
    });
...

This will give this output in the console:

controller.init called
launching app
controller.init called
end launching app
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121

1 Answers1

3

When calling getController inside application.launch, the init method of the controller gets called again, even the controller was already initialized.

I don't know if this is by design or if it is kind a bug, but I found a solution based on the recommendations of @AlexTokarev 'Inter-controller communication'.

Controller:

Ext.define('Mb.controller.Sav', {
    extend: 'Ext.app.Controller',
    init: function(){
        this.listen({
            controller: {
                '*': {
                    savOnLaunch: this.onUserLoaded
                }
            }
        })
    },

Application launch:

Ext.define('Mb.Application', {
    extend: 'Ext.app.Application',
    launch: function() {
        this.fireEvent('savOnLaunch');
    }
Community
  • 1
  • 1
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
  • 1
    Don't call getController() inside `application.launch`, is all. Controllers are loaded and initialized automatically by design. – Alex Tokarev Oct 09 '13 at 16:21