Ember当地的实时名单

时间:2014-05-29 19:12:10

标签: list ember.js each

我尝试使用Ember进行简单的应用程序。索引控制器:

App.IndexController = Ember.ObjectController.extend({
  schools: [{name:"Old school"}],
  actions:{
    add: function(){
        var schools = this.get("schools");
        schools.push({name: 'New school'});
        this.set("schools",schools);
    }
  }
});

索引模板:

<script type="text/x-handlebars" data-template-name="index">
    <button type="button" {{action "add"}}>Add school</button>
    <ul>
      {{#each school in schools}}
         <li>{{school.name}}</li>
      {{/each}}
    </ul>
</script>

当我在开始申请午餐时,我看到:

  • 旧学校

当我点击添加按钮时没有任何反应,为什么?

1 个答案:

答案 0 :(得分:1)

您需要使用pushObject才能让ember知道某个值已添加到列表中。之后没有必要设置它。

add: function(){
    var schools = this.get("schools");
    schools.pushObject({name: 'New school'});
}