How to Delete controller ExtJS?

2019-03-28 06:29发布

问题:

I dynamically create controllers in my application like this:

var loadedController = me.app.getController(controller_name);
            loadedController.init();

How can I delete this controller after using? Thanks.

回答1:

ExtJs currently does not support removal of controllers out of the box. To cleanup a controller, do the following:

  • Extend Ext.app.EventBus with a method uncontrol that unregisters all event listeners that this controller registered on the EventBus. Check out the source of Ext.app.EventBus#control to derive an implementation. Or use this one.
  • Extend Ext.app.Application with a method removeController that removes a given controller instance from the controllers collection. It's a Ext.util.MixedCollection, check out the source for Ext.app.Application#getController. Then clean up all registered listeners for that controller using uncontrol.
  • Implement a destroy method either on your specific controller and/or extend Ext.app.Controller. You should at least call clearManagedListeners() and possibly destroy other objects created by this controller like views or stores, if that suits your application architecture and controller life-cycle.


回答2:

A Premium Member request has been initiated on the Sencha forums. Unfortunately, the original request has been there since August with no word. I've just bumped that request with reference to this thread.

The Sencha Forum Topic (Premium section)



回答3:

You can destroy any Ext class with obj.destroy(). Remember to also remove all references to the object so it can be garbage-collected.



回答4:

getController() method creates a new controller object and stores reference to it in controllers array.

That means, if the controller is loaded using getController(), it will not be deleted, because there will always be at least one reference to it stored in controllers property of Application object.

To load controller in such way that it will be destroyed (free the memory it occupies) as soon as it will lose any references to existing objects (DOM elements, Ext components, its views, etc) one should use Ext.create instead of getController().
Here is the code:

var loadedController = Ext.create(
     me.app.getModuleClassName(controller_name, 'controller'),
     {
         application: me.app,
         id: Ext.id(null, 'controller')
     }
);
loadedController.init(me.app);
loadedController.onLaunch(me.app);

loadedController will be local variable and will get out from scope. The only thing which will continue to bind newly created controller to 'real life' will be the elements the controller creates itself.
For example, controller may create a new view in his onLaunch() method, which will be injected into DOM, and controller will listen to view events using handlers bind in his init() method. As long as view will exist, controller will exist too. But when user closes the view, view will be destroyed and since it was the last reference to our controller, controller will be destroyed, too.

UPDATE
Listeners created with controller are being appended to Ext.app.EventBus and may prevent automatic removal of controller, because they also may keep references to that controller.