ember {{action}}不起作用

时间:2012-11-15 12:39:39

标签: ember.js

我希望按钮在控制器中调用removeAlbum函数。但它没有任何作用。当我点击按钮没有任何反应,没有错误...我该怎么做才能解决这个问题?!

这是我的模板:

<script type="text/x-handlebars" data-template-name="albums">
  {{#if App.albumsController.total}}
    <div><h1>Количество альбомов: {{App.albumsController.total}}</h1></div>
    {{#each content in App.albumsController}}
      <div class='album'>
        <div class='image'>
          <a {{action showAlbum content href=true}}>
            <img {{bindAttr src="content.avatarUrl"}}>
          </a>
        </div>
        <div class='info'>
          <h2>
            <a {{action showAlbum content href=true}}>
              {{content.title}}
            </a>
          </h2>
          <div>{{content.description}}</div>
        </div>
        <div class='actions'>
          <div>
            <button {{action removeAlbum content target="App.albumsController"}}>Delete</button>
          </div>
        </div>
        <div class='clear'></div>
      </div>
    {{/each}}
  {{else}}
    <div><h1>Loading</h1></div>
  {{/if}}  
</script>

这是我的控制者:

App.albumsController = Em.ArrayController.create({
  content: [],
  total: null,
  loadAlbums: function(){
    this.set('content', []);
    this.set('total', null);
    var self = this;
    $.post(App.connection.url, {method: 'albums.getAll', params: {user_id: App.connection.userId}}, function(response){
      self.set('total', response.albums.total);
      response.albums.album.forEach(function(item){
        var buf = App.AlbumInList.create({
          id: item.id,
          title: item.title,
          description: item.description,
          avatarUrl: item.thumbnail_video.thumbnails.thumbnail[1]._content        
        });
        self.pushObject(buf);
      });
    });
  },
  removeAlbum: function(x){
    console.log('remove it');
  }
});

1 个答案:

答案 0 :(得分:1)

尝试将目标设置为控制器而不是App.albumsController。应该解决问题。

<button {{action removeAlbum content target="controller"}}>Delete</button>