PDA

View Full Version : How to unload an MVC controller?



LesJ
18 Mar 2012, 6:06 AM
My application dynamically creates and destroys views. I can dynamically load an MVC controller, but how do I unload it when the view is destroyed and the controller is no longer needed?

mankz
18 Mar 2012, 1:16 PM
Is there a need to clean it up? Controller doesn't implement any destroy method since it doesn't really allocate any resources or create any DOM elements. If it registers listeners on the view, those will be cleaned up when the view is destroyed. So it should only be a simple Object in memory, I doubt there will be much to gain by trying to 'unload' it. I guess you could do:



this.control({ 'viewselector' : { destroy : function() { MyApp.MyController = null; } });

LesJ
18 Mar 2012, 4:48 PM
The controller might still be responding to application events when the view is destroyed. There's just no need for the controller to do anything since there's no view to control. So, I think, there should be an option to suspend the controller until the view is re-created. Another option is to destroy the controller and remove it from the list of controllers so no events are dispatched to it.

mitchellsimoens
19 Mar 2012, 7:30 AM
You can uncontrol the control :) I have a SubAppDemo proof of concept that automatically adds and removes controllers. I add an uncontrol method to the Ext.app.EventBus (works in 4.0.6, haven't updated):

https://github.com/mitchellsimoens/SubAppDemo/blob/master/assets/overrides/Ext.app.EventBus.js

You just pass an array of controller ids to the uncontrol method:


bus.uncontrol([controller.id]);