在动态删除条目后,handlebars模板不会更新以反映计算属性

时间:2013-03-22 01:04:46

标签: ember.js handlebars.js ember-data

以下是基本设置:

  1. 我使用简单的计算属性将一个ember-data模型数组绑定到我的把手模板
  2. 在我通过xhr获取模型之前,我根据某些配置预先添加了一些
  3. 当xhr被解析时,我需要用通过线路返回的对象替换任何配置模型
  4. 这似乎有效,因为在内存中我可以看到我的计算属性只有2个项目,但我的把手模板似乎显示3(其中一个配置模型在xhr返回后实际从数组中删除)< / LI>

    我验证了上面的#&#39;

    a。)在Chrome开发工具中,我要求

      

    App.Day.find(1)获得(&#39;目录&#39)。得到(&#39;长度&#39); //在xhr之后返回2

    b。)我也做了以下

      

    App.Appointment.all()得到(&#39;长度&#39)。 //在xhr之后返回2

    **这里是代码**

    我有以下车把模板(显示3项而不是2项)

    {{#each appointment in day.listings}}
      {{appointment.start}}<br />
    {{/each}}
    

    我日模型中的列表计算属性如下所示

    App.Day = DS.Model.extend({
          name: DS.attr('string'),
          appointments: function() {
              return App.Appointment.find();
          }.property(),
          listings: function() {
              //pretend we need to add some values in memory before we fire the xhr ...
              App.Appointment.add({name: 'first'});
    
              return this.get('appointments');                                     
          }.property().volatile()
      });
    

    约会模型是一个余烬数据模型,但因为我需要动态替换内存中的项目,我重写了find方法(并在我自己的add方法中使用stub来更好地控制数组)

      App.Appointment = DS.Model.extend({
          name: DS.attr('string')
      }).reopenClass({
          records: [],                                                             
          find: function() {
              var self = this;
              self.records.clear();
              $.getJSON('/api/appointments/', function(response) {
                  for(var i = 0; i < response.length; i++) {
                      for(var j = 0; j < self.records.get('length'); j++) {
                          if (self.records[j].get('name') === response[i].name) {
                              //now that our xhr has finished we need to replace any that already exist
                              self.records.splice(j, 1);
                          }
                      }
                  }
              });
              return this.records;
          },
          all: function() {
              return this.records;
          },
          add: function(record) {
              this.records.addObject(App.Appointment.createRecord(record));
          }
      });
    

1 个答案:

答案 0 :(得分:3)

旧答案已删除。远离基地。

splice不符合KVO标准。 replace是符合ember标准的方法。 CollectionView依赖于replace支持的数组变异观察器来了解要添加和删除的视图。

如果这不是问题那么我会吃掉我的帽子。