yorbro
yorbro

Reputation: 1137

Create a new instance of a controller

I have a modal and inside it I'm dynamically rendering views. In ApplicationRoute I have a hook showModal which does the following:

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'
      });
    },
  },
});

You can see that I reset the state of the controller that is rendered inside the modal by calling this.controllerFor(templateName).send('reset'). This works ok but it means that I have to implement this function in every controller that I want to render inside a modal. Therefore, I would like to provide a new "fresh" instance of the controllerFor(templateName) every time the modal is shown. Is there any way to re-create the instance of a controller?

Upvotes: 2

Views: 183

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

Sure, you can register your controller as a non-singleton controller

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

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

Upvotes: 3

Related Questions