如何在Ember框架中销毁和重新创建控制器对象



我正在使用ember 2.12,有两个控制器 - 一个用于申请人,另一个用于我们的应用程序流。当用户决定删除共申请人时,我正在尝试摆脱其状态(不幸的是,它生活在coapplicant Controller中,而不是单独的数据对象(。因此,我试图销毁控制器,然后在下次加载时重新创建它。

在repetController状态中,我致电

this.destroy();

以及在

中的路由/form.js页面中
setupController(controller,model)

我正在检查控制器是否被销毁,我试图重新创建

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

但是,当我这样做时,我会在此时得到错误(...参数(,

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)

我这里有什么我缺少的。

我不想通过手动重置每个字段来清除控制器状态,因为它的逻辑非常复杂(有很多字段,有些是计算的属性,有些属性是根据数据可用性,有条件预填充/未填充的我假设在这种情况下,如果我能以某种方式破坏并重新创建控制器,我可以在无需手动努力的情况下将其状态恢复到原始状态。

不要破坏控制器。在Ember中,控制器是单例。每条路线都有自己的控制器(如果您自己不定义它,Ember将为您创建一个(。删除控制器对象本身将使功能障碍应用程序产生极大的结果。

显然,您得到了一个状态在控制器中的对象。使用setupControllerwillTranstion函数手动重置主要对象。计算的属性会自动进行。

ember无法重新创建控制器。当用户往返此路线时,您可以使用DIDTRANSITION或WILLSTANSITION操作以重置状态。您也可以使用刷新,就像用户"重新访问"路线一样。但是我也确定您的控制器可以并且应该通过将所有"设置"代码移动到可以随时调用的单独方法来重构。

最新更新