3

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

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) 在控制台中记录一个对象数组,这是正确的。我不知道为什么 home 模板中的每个循环都不起作用。

4

1 回答 1

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;
        });
    }
});
于 2013-10-18T12:59:42.920 回答