gpt4 book ai didi

ember.js - 创建一个新的 Controller 实例

转载 作者:行者123 更新时间:2023-12-02 06:04:19 25 4
gpt4 key购买 nike

我有一个模态,在其中我正在动态呈现 View 。在 ApplicationRoute我有一个钩子(Hook)showModal它执行以下操作:

HstryEd.ApplicationRoute = Ember.Route.extend({
actions: {
/* Displays the modal. It adds a CSS class "page-`templateName.dasherize()`"
* modalTitle: Title of the modal window
* templateName: name of the template to render inside the modal. The controller of the modal content will be an instance of a controller of the same name.
*/
showModal: function(modalTitle, templateName) {
// Set the name of the template that will be the content of the modal
this.controllerFor('modal').setProperties({
title: modalTitle,
cssClass: "page-" + templateName.dasherize()
});

// Reset the controller of the modal content
this.controllerFor(templateName).send('reset');

// Render the modal window
this.render('modal', {
into: 'application',
outlet: 'modal',
});
// Render the content inside the modal window
this.render(templateName, {
into: 'modal',
outlet: 'modalContent',
controller: this.controllerFor(templateName)
});
},
/* Hides the modal. */
hideModal: function() {
this.disconnectOutlet({
outlet: 'modalContent',
parentView: 'modal'
});
this.disconnectOutlet({
outlet: 'modal',
parentView: 'application'
});
},
},
});

您可以看到我通过调用 this.controllerFor(templateName).send('reset') 重置了在模态中呈现的 Controller 的状态.这可以正常工作,但这意味着我必须在我想在模态中呈现的每个 Controller 中实现此功能。因此,我想提供 controllerFor(templateName) 的新“新鲜”实例。每次显示模态时。有没有办法重新创建 Controller 的实例?

最佳答案

当然,您可以将 Controller 注册为非单例 Controller

App.register('controller:modal', App.ModalController, {singleton: false });

http://emberjs.jsbin.com/viboc/2/edit

关于ember.js - 创建一个新的 Controller 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22426906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com