Search code examples
sencha-touch-2

How to listen to a event fired by a controller in controller with Sencha Touch2


I have multiple controllers, assume them controller Main and Sub,
Controller Sub fires an event , I want to catch it on controller Main,
what should I do? thanks!

Edit: I paste my code here

in Sub Controller:

config: {

    control:    {
        'pinlist': {
            disclose: 'showPinDetails'                
        }                    
    }

} 


, showPinDetails: function(list, record) {
    console.log('showPinDetails');

    this.fireEvent('showPinDetails',list, record);

}

in Main Controller:

requires: [
            'MyApp.controller.Sub',
            ],  


config: {

    listeners: {
        'showPinDetails': 'showPinDetails',
    }
} , 
showPinDetails: function(list, record){
    console.log('showPinDetails');       
},

So, I just got the log 'showPinDetails' from Sub, rather from the two


Solution

  • In your Sub controller, suppose that you fired an event through something like this:

    yourComponent.fireEvent('my_event',this);
    

    Then you have to define the listener and handler programmatically in your Main Controller. The best practice is:

    Ext.define('app.controller.Main', {
        extend: 'Ext.app.Controller',
        config: {
            refs: {
                   yourComponent: 'enter your selector here, eg. #id'
            },
            control: {
                   yourComponent: {
                     'my_event': 'my_event_handler'
                   }
            }
        },
        my_event_handler: function() {
          //do whatever you want here
        })
    });