PDA

View Full Version : Controller -- sub-classing and event handling.



santosh.kumar
14 Feb 2013, 7:50 AM
Hi,

I have a question about the right approach to handle events in derived controllers. Here is my code:



Ext.define('ControllerA', {
extend: 'Ext.app.Controller',

refs: [
{
selector: 'configpanelselector',
ref: 'configpanel'
}],


init: function() {

var me = this;

this.control({

'configpanelselector': {
activate: function () {

me.loadData();


}
}

});


},


loadData():function{

me.getConfigpanelSelector().loadData();

}

getConfigpanelSelector:function () {
var p = Ext.ComponentQuery.query('#ControllerA' + 'configpanel');
return p[0];
}


}




Ext.define('ControllerB', {
extend: 'ControllerA',

init: function() {

this.callParent();

},

getConfigpanelSelector:function () {
var p = Ext.ComponentQuery.query('#ControllerB' + 'configpanel');
return p[0];
},


}





1. For ControllerA and ControllerA i am creating different configpanels (different instances) in different some other controller. (say, ViewA and ViewB respectively for ControllerA and ControllerB )

2. In ControllerA activate event fires for both views, which is fine. But in the loadData-> me.getConfigpanelSelector() always gets ControllerA' view (which is viewA).

3. To get around point #2, i ovveride getConfigpanelSelector() function in both controllers and returning the right instance by querying the component with id.

4. Now the questions:

a. load still gets called on both views as the event is there in two Controllers. How to avoid that?
b. is my approach of sub-classing correct?


Thanks
Santosh

mitchellsimoens
17 Feb 2013, 8:19 AM
loadData will fire in both controllers because each will execute the me.control from the ControllerA so each will get it's own listener for the activate event. Usually a superclass controller doesn't get added to the controllers in Ext.application for these reasons. I'd have a 3rd controller named MyApp.controller.Abstract that ControllerA and ControllerB would extend and the Abstract controller do the things that are to be shared among both controllers like your ref getter override. But then ControllerA would add the activate listener as it would want it but ControllerB doesn't so that listener isn't a shared listener. You would also not add the Abstract controller to the controllers array in Ext.application.

santosh.kumar
19 Feb 2013, 11:41 AM
i want the activate event to fire in both controllers but for their own views. eg. ControllerA's activate should fire only when 'ControllerAView' is activated and respectively. The problem is because the views for both the controllers are of same type (thought different instances) controllers getting event.

Is there a way i can register for event with the Id of the view?




this.control({

'ControllerAView_id': {
activate: function () {

me.loadData();




}
}

});




(ControllerB also register event with 'ControllerBView_id')

1. if yes, can i do dynamic registration of that event? Because the Id of the view could be different in different context.