PDA

View Full Version : Reuse Views With Multiple Controllers



arandlett
28 Dec 2011, 4:28 PM
I am trying to reuse views with multiple controllers. The problem I run into is that the controllers are listening to the same view events. Instead of defining views in controller to I do Ext.create() within the controller init function?

mitchellsimoens
29 Dec 2011, 8:48 AM
A controller listens to any event that is fired based on the ComponentQuery selector you provided.

Say you have a form with some checkboxes and you want to do something with the checked event. Since you may have 100 checkboxes but the logic is the same you can use one selector:


this.control({
'formpanel checkboxfield' : {
checked : someFn
}
});

That will listen to any checkboxfield that is a child of a formpanel no matter when it is created. So you can remove and add checkboxfields and formpanels all day and this code should still listen to them

arandlett
29 Dec 2011, 9:06 AM
So if you have multiple instances of a view the controller should listen to events from all of those instances. If you setup a reference in the controller it should know which instance the event was fired from? So all that too say I could just use the one controller for each instance of those views?

mitchellsimoens
29 Dec 2011, 9:08 AM
Yes, anything that matches that selector will be listened to. The first argument of a listener will be the component firing the event.

I usually don't architect my controllers based on a view... I have controllers based on feature/function. Technically you can have just one controller but that isn't very good application architecture.

arandlett
29 Dec 2011, 9:16 AM
I am trying to reuse code. I have multiple tabs that serve up similar content ( list > detail ) so the functionality will be the same for each. Would you still create multiple views for each tab of content even though it is the same?

mitchellsimoens
29 Dec 2011, 9:22 AM
A tabpanel has to have multiple instances. You can use the same component but separate content. Then your controller action will most likely be the same.