Robin Bajaj
Robin Bajaj

Reputation: 2072

how to destroy and recreate a controller object in Ember framework

I am using Ember 2.12, there are two controllers - one for applicant and other for coapplicant for our application flow. When the user decides to delete the coapplicant, I am trying to get rid of its state (that unfortunately lives in the coapplicant controller instead of a separate data object). so i am trying to destroy the controller and then recreate it the next time the page loads.

in the resetController state, I call

this.destroy();

And in the routes/form.js page, in the

setupController(controller,model)

I am checking if the controller is destroyed, i tried to recreate it

if (controller.isDestroyed) {
        Ember.Logger.error('controller is already destroyed');
        this.set('controller',FormEditCoapplicantController.create());
    }
    this._super(...arguments);

but when i do that, I get the error when this._super(...arguments) is called,

Error while processing route: form-edit-coapplicant Assertion Failed: calling set on destroyed object: <account-open@controller:form-edit-coapplicant::ember3345>.model = [object Object] Error: Assertion Failed: calling set on destroyed object: <account-open@controller:form-edit-coapplicant::ember3345>.model = [object Object]
at assert (http://localhost:4200/assets/vendor.js:21056:13)
at Object.assert (http://localhost:4200/assets/vendor.js:32807:34)
at Object.set (http://localhost:4200/assets/vendor.js:37553:22)
at Class.setupController (http://localhost:4200/assets/vendor.js:42366:21)
at Class.setupController (http://localhost:4200/assets/vendor.js:207076:9)
at Class.superWrapper (http://localhost:4200/assets/vendor.js:55946:22)
at Class.setupController (http://localhost:4200/assets/account-open.js:6331:16)
at Class.superWrapper [as setupController] (http://localhost:4200/assets/vendor.js:55946:22)
at Class.setupController (http://localhost:4200/assets/account-open.js:7184:16)
at Class.superWrapper [as setupController] (http://localhost:4200/assets/vendor.js:55946:22)

Is there anything that i missing here.

I don't want to clear the controller state by manually resetting each field because the logic for that is very complex (there are lots of fields, some are computed properties, some conditionally prefilled/not-prefilled depending on data availability, I am assuming if i can somehow destroy and recreate the controller in this case, i can get the brand new created controller with its state back to original without much manual effort.

Upvotes: 0

Views: 979

Answers (2)

Gennady Dogaev
Gennady Dogaev

Reputation: 5991

Ember does not have a way to recreate controller. You can use didTransition or willTransition actions of according route to reset state when user navigates to or from this route. You can also use refresh and it will be like if user "revisits" route. But I'm also sure your controller could and should be refactored by moving all "setup" code to separate method which you can call anytime.

Upvotes: 1

Jacob van Lingen
Jacob van Lingen

Reputation: 9537

Don't destroy the controller. In Ember, controllers are singletons. Every route has it's own controller (if you do not define it yourself, Ember will create one for you). Removing the controller object itself will in all probability result into a dysfunctioning application.

Apparently, you got an object which state is held within the controller. Use the setupController or willTranstion functions to manually reset the main object. Computed properties automatically go along.

Upvotes: 1

Related Questions