灰烬 - 重新渲染模板时遇到问题

时间:2014-02-04 09:17:16

标签: ember.js handlebars.js

我遇到让hbs知道房产已经发生变化的问题。 我试过“propertyDidchange”等。似乎没什么用。

我有一个表单计数器

personFormCount: [{id: 1}, {id: 2}]

并形成hbs

{{#each personFormCount}}
      <br>Form ID: {{id}}
      <button type="submit" {{action 'getID' id}}>ID</button> // sends ID
      {{view 'person'}}
{{/each}}

JS

getID: function(id){
          var index = id - 1;
          this.get('personFormCount').splice(index, 1);
          this.notifyPropertyChange('personFormCount');
    },

模板上没有发生任何变化。触发操作时,数组会自动更改。功能应该注意删除。

1 个答案:

答案 0 :(得分:1)

splice确实改变了数组,但是Ember没有注意看你是否在拼接数组。你真的想要使用内置的删除功能。

http://emberjs.com/api/classes/Ember.ArrayProxy.html#method_removeObjects

http://emberjs.com/api/classes/Ember.ArrayProxy.html#method_removeObject

http://emberjs.com/api/classes/Ember.ArrayProxy.html#method_removeAt

在你的情况下,这将是一个很好的实现

{{#each personFormCount}}
      <br>Form ID: {{id}}
      <button type="submit" {{action 'removeItem' this}}>ID</button> // removes this item
      {{view 'person'}}
{{/each}}


removeItem: function(item){
      this.get('personFormCount').removeObject(item);
},
相关问题