简体   繁体   中英

Ember - Object has no method 'map'

I am going through the ember.js guides to learn Ember, and I am using the latest builds of ember.js , ember-data.js , and handlebars.js. I am able to successfully set up a basic nav that switches between views. However, upon trying to integrate a model with a {{#each model}} , I get an error message: Uncaught TypeError: Object # has no method 'map'

Many people seem to have asked a related question, but the solution has always been to update the version of ember, which I have already done.

I believe have followed the tutorial precisely and my code so far is as follows.

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'
});

And the relevant markup:

<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>

All help is much appreciated, and sorry if the formatting of this question is awful - it's my first question! Cheers!

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}}

The main issue here is that your {{#each}} statement is a little malformed, you should be iterating over the controller object like so:

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

This is because Ember controllers act aa proxy to their object or—in the case of Ember.ArrayController —to their array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM