View Full Version : Many instances of one controller, how to do?

16 Oct 2011, 6:44 PM

For a very simple MVC application, the framework is really easy and straightforward, but in slightly more complicated scenarios, I feel like the framework is not quite ready yet. There are too many questions in my mind, too many ways of doing, but there is no obvious and clean ones...

I'm creating an application in which you have a list of data on the left, and when you double click on it, basically I'd like to instantiate a new User controller with associated views. Its main view will be added as a tab, then the new controller will take care of those views, independently of the rest.

The truth is I don't think there is a simple way of doing that, still, it's a simple case, right ?

How would you do it?

17 Oct 2011, 3:05 AM
You just associate the controller to your application. It will have a single controller.
When you create a new view, the (only one) controller you control it too.
You just have to create the view.

17 Oct 2011, 3:42 AM
like said previowsly you just add the tabs (views), to the tab-panel, the controller will control them all :D. keep in mind the controller is shared so YOU SHOULD NOT save view properties inside the controller, for obvious reasons each view state and properties should be inside each view. when the controller receives events you have the view as parameter so you can control each view individually.

17 Oct 2011, 8:24 PM
Basically, what you both said is that, if I set for example "views: [ User ]" in a controller then later on (somewhere in that controller) I instantiate a new "User" view, all the refs / this.control will be regenerated and will work?!

18 Oct 2011, 12:13 AM
If I understood well, I should have this:

--- VIEW 1
--- VIEW 2
--- VIEW x

Now, the main controller still have to create the views itself (user views and company views), but how does it know that such views exist? It only knows the controllers.

If we let the "children" controllers to create those views instead, then they would have to listen to events occurring in the viewport (which has a different controller), and they would have to "fight" to decide who is going to take care of it (for instance, to create the appropriate view), and to add it to the adhoc container.

Both ideas are bit disturbing for me :p

What I do now is this :

--- VIEW 1
--- VIEW 2
--- VIEW x

Basically, the main controller listen to some main events, then decide which controller to instantiate. It calls the init passing the container as an argument. That way, the child controller does his job independently and creates the view(s) in the container. I can use this controller + the associated views in another project easily.

I'm not sure what is best, and how we're supposed to use the framework in a big project, but this is how I'm doing it. Please tell me if you think it's completely wrong ~o)

18 Oct 2011, 3:32 AM
I'm sorry. Everytime i read you post i understand a different thing. My bad.
Your question is about having a controller for a group of views, like having one controller to listen to a viewport, menu, main panel and control all of it at same place?
Or it's to when you will create the same view many times, like opening a view to edit records with no limits about how many records you can edit at same time?

18 Oct 2011, 6:39 PM
Sorry, my post is probably not clear enough :(

like having one controller to listen to a viewport, menu, main panel and control all of it at same place
Well, that also, even though it's not really said in my posts before (as my issue isn't here). My application has a main controller (I call it the Coordinator), and views associated with it (viewport, menu, header, and... the so called 'module container'). This part is easy, and all the examples using ExtJS MVC are made on such a structure. My problem comes right after that.

will create the same view many times, like opening a view to edit records with no limits about how many records you can edit at same time
Yes! Now, from that main controller and main views, I can start new screens, which depend on different controllers. For example, I can double-click on a user or a company, and depending on this I will instantiate the User Controller or Company Controller (passing the Module Container as an argument). The controller will take care of its own views and add its main one in the module container (as a tab generally).

Now that's the way I do it. I wonder if it's the best way or not :-/

19 Oct 2011, 2:44 AM
What I do here is all the controllers are assigned on my app(Ext.application). The main controller to all the view controllers.
The view is assigned to each controller.
The main controller will take care of opening the view. You will not create the controller, but just create the view.
When you create the view, Ext will automatically register the controller to be listening to your view and you create how many views you want. The view will be controlled for the right controller because inside the controller you put your view.
You have to take care because I'm almost sure the controller for the view is a singleton, which means you will have 1 controller to N views.
So, just add the controller on you app and add each view to each controller. After that, on the main controller you will create the view (on a window or on a tabpanel)

19 Oct 2011, 7:45 PM
When you create the view, Ext will automatically register the controller to be listening to your view.
I didn't actually new that the MVC framework was THAT dynamic ;) It's cool.

The only thing that I don't really like, is the fact to declare the views in the controllers (except the main one), but to create the view from the main controller, which initially don't really 'know' about them. How do you create the view from the main controller exactly? Something like getController('xController').getView('xView1').create()?

22 Oct 2011, 1:49 PM
You probably wants to create a view when the user clicks on something, right?
Here, it will happen when we click on a treepanel (our menu).
The main controller listen to this treepanel and will be the responsible to "create" the view
We have a tabpanel which is the place where we'll open our views.
When the user clicks on the menu, I get the xtype which will be created and create and configuration

var myConfig = {xtype : 'myView1', title : '', ...};
I get the tabpanel to add my configuration

var tabpanel = tree.up('viewport').down('tabpanel');
We don't need to create the component. When we add it into a tabpanel, Ext automatically creates it.
If you open it in a window, it will have the same behavior.