View Full Version : Event handling [control() method best practices]

Mark Hagerman
18 Jun 2012, 9:46 AM
I'm developing code that creates a form.panel inside a panel. The form panel contains a checkbox; if the user clicks the checkbox, the application responds by creating a new instance of the form panel and adding it into the container. At present, my event listener for the checkbox' change event resides in the container; it seems to me I should move that functionality into a controller.

The question is this: Can I use the controller's control() method to establish the listener(s) for the checkbox? I would expect not, since the checkboxes (other than the first one, perhaps) won't exist at the time the control() method is run. Should I continue to dynamically add the change listener to each checkbox as it's created, or is there a better way?

18 Jun 2012, 7:21 PM
I don't know exactly what you are trying to achieve and this is not a direct answer to your question, but I have an important rule when I develop something with mvc: when you have a component which you need to hook to an event, if the event when is fired will only edit the interface in some way (and not sending requests/doing things), you should hook to that event in the view and work through it, not on the controller.

The controller shouldn't be polluted with useless methods, you should put there only things that interacts with your stores.

Hope this help a bit.

Notice that you can't have a reference to the controller from the view, so yea control method is for "static" created components.

However, a good way to handle this could be:
- Hook in the controller to your first checkbox
- When is checked, defer to the view the creation of the form panel with everything you need and return objects that you require to hook events to in the controller.
- Hook to those events with .control

I think this is the correct way to handle this situation