创建控制器的新实例

时间:2014-03-15 17:01:56

标签: ember.js

我有一个模态,在里面我动态渲染视图。在ApplicationRoute我有一个钩子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')来重置在模态内呈现的控制器的状态。这工作正常,但这意味着我必须在每个我想在模态内渲染的控制器中实现此功能。因此,我想提供一个新的"新鲜"每次显示模态时controllerFor(templateName)的实例。有没有办法重新创建控制器实例?

1 个答案:

答案 0 :(得分:3)

当然,您可以将控制器注册为非单件控制器

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

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