chopper
chopper

Reputation: 6709

How to access child controller of hasMany

I would like to trigger an action on the ArrayController of child elements in the parent view. How would I go about that?

Specifically, I am trying to add a new child element to a hasMany relationship. It works fine using {{action new}} in the view of the child elements, but I am not sure how to do this from the parent view.

<script type="text/x-handlebars" data-template-name="parents">
    <b>List</b><br />
    {{#each parent in controller}}
        {{ parent.name }} <br />    
        {{ render "kids" parent.children }}
    <a href="#" {{ action parent.children.new target="controller" }}>Parent View Make Kid</a<br/>
    <br />
    {{/each}}
</script>

<script type="text/x-handlebars" data-template-name="kids">
    {{#each kid in controller}}
        > {{ kid.name }} <br />
    {{/each}}
    <a href="#" {{ action new }}> Kid Controller Make Kid</a><br />
</script>

In the above "Kid Controller Make Kid" works fine, "Parent Controller Make Kid" does not.

Please find a fiddle illustrating the problem here: http://jsfiddle.net/chopper/zHnQC/2/

Thanks!

Upvotes: 0

Views: 351

Answers (1)

danii
danii

Reputation: 5693

From the parent's view you can't access directly to the children's controller like that. I think the best option would be to implement a newKid method on ParentsController responsible to create the new record on its model.

I've forked your fiddle and implemented a working example: http://jsfiddle.net/pxkys/1/

Hope this helps!

Upvotes: 2

Related Questions