1

I am having a problem adding an array controller as an item controller of another array controller.

Error I am getting is:

Error while loading route: TypeError {} ember.min.js:15
Uncaught TypeError: Object # has no method 'addArrayObserver'

JSFiddle: http://jsfiddle.net/t5Uyr/3/

Here is my HTML:

<script type="text/x-handlebars">
    <table>
        <thead>
            <tr>
                <th>id</th>
                <th>items</th>
            </tr>
        </thead>
        <tbody>
            {{#each}}
            <tr>
                <td>{{id}}</td>
                <td>
                    <ul>
                    {{#each items}}
                        <li>{{formattedName}}</li>
                    {{/each}}
                    </ul>
                </td>
            </tr>
            {{/each}}
        </tbody>
    </table>
</script>

As you can see, inside the template I iterate over a collection of data with each loop, inside the each loop I want to iterate over a subcollection of the data.

Here is my JS code:

window.App = Ember.Application.create({});

App.ApplicationRoute = Ember.Route.extend({

    model: function () {

        var data = [
            {
                id: "111",
                items: [
                    {
                        name: "foo"
                    },
                    {
                        name: "bar"
                    }
                ]
            },
            {
                id: "222",
                items: [
                    {
                        name: "hello"
                    },
                    {
                        name: "world"
                    }
                ]
            }
        ];

        return data;

    }

});

App.ApplicationController = Ember.ArrayController.extend({

    itemController: "row"

});

App.RowController = Ember.ArrayController.extend({

    itemController: "item"

});

App.ItemController = Ember.ObjectController.extend({

    formattedName: function () {
        return "My name is " + this.get("name");
    }.property("name")

});

1 Answer 1

2

App.RowController should be an objectController your items (rows) are objects with an array in one of their properties and not arrays themselves... You can assing the controller in the inner each directly and remove itemController from the App.RowController.

JavaScript

App.RowController = Ember.ObjectController.extend()

Handlebars

{{each items itemController='item'}}

JsFiddle http://jsfiddle.net/mUJAa/3/

Sign up to request clarification or add additional context in comments.

3 Comments

Can you be more specific? You have mixed JavaScript and handlebars code in that snippet? Where should the {{each... go? Can you show me in my JSFiddle?
I tried that and it'w not working very well. Could you take a look at my jsfiddle please?
Thanks. I tried that. But there is an issue with computed property on the item controller not working though: jsfiddle.net/mUJAa/2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.