Ember:断言失败:#each循环的值必须是一个数组。你通过了(生成的家庭控制器)

时间:2013-10-18 11:05:09

标签: ember.js

我在控制台中收到此错误:

Assertion failed: The value that #each loops over must be an Array. You passed (generated home controller)
Uncaught TypeError: Object [object Object] has no method 'addArrayObserver'

我的HTML看起来像这样:

<script type="text/x-handlebars">
    <div id="top-panel">
        <h1>{{{title}}}</h1>
    </div>
    <div id="wrapper">
        <div id="content">
            {{outlet}}
        </div>
    </div>
</script>

<script type="text/x-handlebars" id="home">
    <table>
        <thead>
        <tr>
            <th>Id</th>
            <th>Foo</th>
            <th>Bar</th>
            <th>Foo Bar</th>
        </tr>
        </thead>

        <tbody>
        {{#each}}
        <tr>
            <td>{{itemId}}</td>
            <td>foo</td>
            <td>foo</td>
            <td>foo</td>
        </tr>
        {{/each}}
        </tbody>
    </table>
</script>

我已经定义了这样的家庭路线:

App.Router.map(function () {
    this.resource("home", { path: "/" });
});

App.HomeRoute = Ember.Route.extend({
    model: function () {
        $.getJSON("mocks/items.json", function (items) {
            console.log(items);
            return items;
        });
    }
});

console.log(items)在控制台中记录一组对象,这是正确的。我不知道为什么主页模板中的每个循环都不起作用。

1 个答案:

答案 0 :(得分:2)

找出问题所在。我忘了在路线中返回promise对象:

App.HomeRoute = Ember.Route.extend({
    model: function () {
        $.getJSON("mocks/items.json", function (items) {
            console.log(items);
            return items;
        });
    }
});

应该是:

App.HomeRoute = Ember.Route.extend({
    model: function () {
        return $.getJSON("mocks/items.json", function (items) {
            console.log(items);
            return items;
        });
    }
});