在Ember中如何从ArrayController中删除对象

时间:2012-12-10 05:52:25

标签: arrays ember.js

我正在执行以下操作,但我无法从方法thissearchAgain中的上下文中检索removeUser obj。这是我从http://www.adobe.com/devnet/html5/articles/flame-on-a-beginners-guide-to-emberjs.html

获取的示例
     <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.contextSearchAgain内的removeUser给了我未定义的内容。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案;我希望这也有助于其他人:

  • recentUsersController实际接收事件的方法,而不是视图(我相信文章中的错误)
  • {{each}}小胡子应该读{{#each user in App.recentUsersController.reverse}},以便声明您正在循环的对象的标识符
  • 在循环recentUsers时,应在{{action}}胡须内添加此类标识符(例如{{action "removeUser" "user" target="App.recentUsersController"}}
相关问题