Ember.js - 第一个简单的教程不起作用

时间:2012-10-21 11:10:21

标签: ember.js

我写了第一个Ember.js应用程序,但它无法正常工作。

我的index.html

                                                     

  <script type="text/x-handlebars">
      {{#if Songs.totalReviews}}
          Read all my {{Songs.totalReviews}} reviews!
      {{else}}
          There are no reviews right now.
      {{/if}}

      {{#each Songs.songsController}}
          <h3>{{title}}</h3>
          <p>{{artist}} - {{genre}}</p>
      {{/each}}
  </script>

  </body>
  </html>

我的app.js

      Songs = Ember.Application.create({
        mixmaster: 'Andy',
        totalReviews: 1,
        ready: function(){
            alert('Ember sings helloooooooooo!');
        }
      });

      Songs.Song = Ember.Object.extend({
        title: null,
        artist: null,
        genre: null,
        listens: 0
      });

      mySong = Song.create({
        title: 'Son of the Morning',
        artist: 'Oh, Sleeper',
        genre: 'Screamo'
     });

      Songs.songsController = Ember.ArrayController.create({
      content: [],
      init: function(){
          // create an instance of the Song model
          var song = Songs.Song.create({
              title: 'Son of the Morning',
              artist: 'Oh, Sleeper',
              genre: 'Screamo'
          });
          this.pushObject(song);
        }
      });

但如果我打开这个页面,我就得到了

 Read all my 1 reviews!

歌曲阵列怎么样?我是以错误的方式构建的吗?

2 个答案:

答案 0 :(得分:3)

问题在于以下代码段:

  mySong = Song.create({
    title: 'Son of the Morning',
    artist: 'Oh, Sleeper',
    genre: 'Screamo'
 });

此处Song未定义u shud使用Songs.Song.create代替....修改代码&amp;一切正常,这里是fiddle

答案 1 :(得分:0)

似乎问题是{{#each Songs.songsController}}

应该使用{{#each Songs.songsController.content}}

问题解决了。