Question

I have a template templates/post.hbs with this content:

<h2>{{title}}</h2>
<div>
  {{#link-to 'post.edit' this}}Edit{{/link-to}}
  <a {{action 'destroy'}}>Destroy</a>
</div>
{{text}}


{{ render 'comments/new' }}

<h3>Comments</h3> 

{{#each comments}}
  <strong>{{author}}</strong>
  {{text}}
{{/each}}

The comments/new form near the bottom there has content like this:

<h3>Leave a comment</h3>
<form>
  <p class='input-group'>
    <label for='text'>Text</label><br>
    {{textarea value=text class="form-control" rows="10" cols="50"}}
  </p>
  <input type='button' value='Add Comment' class='btn btn-primary' {{action 'save' this}}>
</form>

Where and how do I set up a Blorgh.Comment object to be used in this form? Does it go into the PostRoute or the CommentsNewController? If so, what's the syntax?

Était-ce utile?

La solution

There are a couple of ways you can accomplish this, though I'd suggest one of the following:

  1. Set the model in CommentsNewController's init method. It would look something like this:

    App.CommentsNewController = ObjectController.extend({
      init: function() {
        this.set('model', App.Comment.create());
      }
    });
    
  2. Bind the new comment to a controller property in PostRoute and then pass it to the render helper, like so:

    {{render 'comments/new' comment_object }}
    

You could even maybe use an outlet instead and do it all in PostRoute#setupController.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top