2012-12-10 27 views
0

我正在做以下操作,但我無法從方法searchAgainremoveUser中的上下文中檢索this obj。這是我正在採取的示例http://www.adobe.com/devnet/html5/articles/flame-on-a-beginners-guide-to-emberjs.html在Ember中如何從ArrayController中刪除對象

 <div id="recent"> 
      <h3>Recent Users</h3> 
      <ol> 
       {{#each App.recentUsersController.reverse}} 
        <li> 
         <a href="#" title="view again" {{action "searchAgain" target="App.recentUsersController"}}>{{this}}</a> - 
         <a href="#" title="remove" {{action "removeUser" target="App.recentUsersController"}}>X</a> 
        </li> 
       {{/each}} 
      </ol> 
     </div> 


    App.recentUsersController = Em.ArrayController.create({ 
content: [], 
addUser: function(name) { 
    if (this.contains(name)) this.removeObject(name); 
    this.pushObject(name); 
}, 
removeUser: function(view){ 
    alert(view.context); 
    this.removeObject(view.context); 
}, 
searchAgain: function(view){ 
    alert(view.context); 
    App.tweetsController.set('username', view.context); 
    App.tweetsController.loadTweets(); 
}, 
reverse: function(){ 
    return this.toArray().reverse(); 
}.property('@each') 

});

view.context裏面的SearchAgainremoveUser給我未定義。有人可以幫助我嗎?

回答

0

我找到了解決方案;我希望這種幫助別人太:

  • 在該recentUsersController的方法實際接受的事件,而不是一個視圖(我相信這是在文章中的錯誤)
  • {{each}}鬍子應該閱讀{{#each user in App.recentUsersController.reverse}},以通過
  • 聲明爲要循環的對象的標識符的同時通過循環recentUsers,這樣的標識符應該在{{action}}小鬍子內被添加(例如{{action "removeUser" "user" target="App.recentUsersController"}}