1

我正在通过 ember.js 指南来学习 Ember,并且我正在使用ember.jsember-data.js和 handlebars.js 的最新版本。我能够成功设置在视图之间切换的基本导航。但是,在尝试将模型与 a 集成时{{#each model}},我收到一条错误消息: Uncaught TypeError: Object # has no method 'map'

好像很多人都问过一个相关的问题,但是解决办法一直是更新ember的版本,我已经做过了。

我相信已经准确地遵循了教程,到目前为止我的代码如下。

App = Ember.Application.create();

App.Store = DS.Store.extend({
    revision: 12,
    // Says we are specifying all models in js
    adapter: 'DS.FixtureAdapter'
});

App.Router.map(function() {
    this.resource('posts');
    this.resource('about');
});

App.PostsRoute = Ember.Route.extend({
    model: function() {
        return App.Post.find();
    }
});

App.Post = DS.Model.extend({
    title: DS.attr('string'),
    author: DS.attr('string'),
    intro: DS.attr('string'),
    extended: DS.attr('string'),
    publishedAt: DS.attr('date')
});

App.Post.FIXTURES = ({
    id: 1,
    title: 'Book Title',
    author: 'Dave',
    publishedAt: new Date('12-27-2012'),
    intro: 'This is an introduction to the book',
    extended: 'This is an even longer introduction to the book'
}, {
    id: 2,
    title: 'Book Title 2',
    author: 'James',
    publishedAt: new Date('08-13-2012'),
    intro: 'This is an introduction to another book',
    extended: 'This is an even longer introduction to another book'
});

以及相关的标记:

<script type="text/x-handlebars">
<div class="navbar">
    <div class="navbar-inner">
        {{#linkTo 'index' classNames='brand'}}Brand{{/linkTo}}
        <ul class="nav">
            <li>{{#linkTo 'about'}}About{{/linkTo}}</li>
            <li>{{#linkTo 'posts'}}Posts{{/linkTo}}</li>
        </ul>
    </div>
</div>
{{outlet}}
</script>

<script type="text/x-handlebars" id="about">
<div class="about">
<p>Here is some text about the page</p>
</div>
</script>

<script type="text/x-handlebars" id="posts">
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span3">
            <table class='table'>
            <thead>
                <tr><th>Recent Posts</th></tr>
            </thead>
            {{#each model}}
            <tr>
                <td><a href="#">{{title}} <small class='muted'>by {{author}}</small></a></td>
            </tr>
            {{/each}}
            </table>
        </div>
        <div class="span9">
        </div>
    </div>
</div>
</script>

非常感谢所有帮助,如果这个问题的格式很糟糕,我们很抱歉 - 这是我的第一个问题!干杯!

4

2 回答 2

0

Try using controller instead of model to iterate over the models in an array controller.

        {{#each controller}}
           <tr>
               <td><a href="#">{{title}} <small class='muted'>by {{author}}</small></a></td>
           </tr>
        {{/each}}
于 2013-05-24T21:33:12.147 回答
0

这里的主要问题是您的{{#each}}语句格式有点错误,您应该像这样迭代controller对象:

{{#each controller}}
  <tr>
    <td><a href="#">{{title}} <small class='muted'>by {{author}}</small></a></td>
  </tr>
{{/each}}

这是因为 Ember 控制器充当其对象的代理,或者 - 在这种情况下Ember.ArrayController- 其数组。

于 2013-05-25T04:21:20.437 回答