DelphiLynx
DelphiLynx

Reputation: 921

Ember.js {{render}} helper model not correctly set

I ran in a strange issue. I have an Article template. Within the article template I call a {{render "category/new" category}}.

However when saving the new Category trough an action the wrong model is used. When I change it to {{render "category/new" this}} it uses the Article model. When I leave the model part empty it does also not work.

The Template:

<script type="text/x-handlebars" data-template-name="article">
    ((...))
    {{render "category/new" category}} // calls the popup for adding a new category
    ((...))
</script>

<!-- popups -->
<script type="text/x-handlebars" data-template-name="category/new">
    <div class="popup">
        <h2>Add new category</h2>

        Name: {{input type="text" value=name}}<br />
        Image: {{view App.UploadFile name="image" file=image }}<img {{bind-attr src=image}}><br />
        Category-parent: {{input value=categoryRelation}}<br />

        <button {{action 'saveCategory'}}>Save</button>
        </div>
</script>

The Router:

// both routes have the render called, it uses the same template
this.resource('article', {path: '/article/:id'}); 
this.resource('article.new', {path: "/article/new"});

The Model:

App.Category = DS.Model.extend({
    name: DS.attr('string'),
    image: DS.attr('string'),
    categoryRelation: DS.belongsTo('category')
});

App.Article = DS.Model.extend({
    name: DS.attr('string'),
    category: DS.hasMany('category')
)};

The Controller:

App.CategoryNewController = Ember.ObjectController.extend({
    actions: {
        saveCategory: function () {
            console.log('CategoryNewController saveCategory action'); // gets called
            console.log(this.get('model')); // the wrong one
            this.get('model').save(); // saves all categories when using {{render "category/new" category}} 
        }
    }
});

Please note, there is no route for Category/new because it is not needed for the {{render}} helper. See: http://emberjs.com/guides/templates/rendering-with-helpers/#toc_specific (see table at the bottom of the page)

Upvotes: 1

Views: 1019

Answers (1)

Michael Johnston
Michael Johnston

Reputation: 5320

On Article, category is a has-many, so you are setting the model of CategoryNewController to an array of Category (unless you've left out a part that creates a Category object when the user clicks new or something.)

Upvotes: 2

Related Questions