如何在Ember框架中销毁并重新创建控制器对象 [英] how to destroy and recreate a controller object in Ember framework

查看:72
本文介绍了如何在Ember框架中销毁并重新创建控制器对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在resetController状态下,我叫

  this.destroy(); 

在route / form.js页面的

  setupController(controller,model)

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

  if(controller.isDestroyed){
灰烬.Logger.error('控制器已经被销毁');
this.set(‘controller’,FormEditCoapplicantController.create());
}
this._super(... arguments);

但是当我这样做时,this._super(... arguments)为

 处理路由时出错:form-edit-coapplicant断言失败:调用了销毁对象上的set:< account-open @controller:form-edit-coapplicant :: ember3345> .model = [对象对象]错误:断言失败:调用销毁对象上的set:< 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)Object.set上的
(http:// localhost:4200 / assets / vendor.js:37553:22)Class.setupController上的
(http:// localhost:4200 / asset / vendor.js:42366:21)
在Class.setupController(http:// localhost:4200 / assets / vendor.js:207076:9)
在Class.superWrapper(http:// localhost:4200 / assets / vendor.js:55946:22)
在Class.setupController(http:// localhost:4200 / assets / account-open.js:633 1:16)Class.superWrapper上的
[作为setupController](http:// localhost:4200 / assets / vendor.js:55946:22)Class.setupController上的
(http:// localhost: 4200 / assets / account-open.js:7184:16)
在Class.superWrapper [作为setupController](http:// localhost:4200 / assets / vendor.js:55946:22)

这里有什么我想念的东西吗?



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

解决方案

不要破坏控制器。在Ember中,控制器是单例。每条路由都有其自己的控制器(如果您自己未定义的话) ,Ember会为您创建一个。)删除控制器对象本身很可能会导致功能失常的应用程序。



显然,您得到了一个状态保持在其中的对象使用 setupController willTranstion 函数来手动重置主对象。

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.

解决方案

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.

这篇关于如何在Ember框架中销毁并重新创建控制器对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆