jeudi 8 février 2018

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.




Aucun commentaire:

Enregistrer un commentaire