gpt4 book ai didi

ember.js - 动态删除条目后, Handlebars 模板不会更新以反射(reflect)计算属性

转载 作者:行者123 更新时间:2023-12-02 08:00:13 30 4
gpt4 key购买 nike

这是基本设置:

  1. 我使用简单的计算属性将 ember-data 模型数组绑定(bind)到我的 Handlebars 模板
  2. 在通过 xhr 获取模型之前,我会根据某些配置添加一些模型
  3. 当 xhr 解决后,我需要用通过网络返回的对象替换任何配置模型
  4. 这似乎确实有效,因为在内存中我可以看到我的计算属性只有 2 个项目,但我的 Handlebars 模板似乎显示 3 个(在 xhr 返回后实际从数组中删除的配置模型之一)

我验证了上面的#,如下

a.) 在 chrome 开发工具中我要求

App.Day.find(1).get('listings').get('length'); //returns 2 after the xhr

b.)我还做了以下操作

App.Appointment.all().get('length'); //returns 2 after the xhr

** 这是代码 **

我有以下 Handlebars 模板(显示 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()
});

约会模型是一个 ember-data 模型,但因为我需要动态替换内存中的项目,所以我重写了 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));
}
});

最佳答案

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

splice 不符合 KVO 标准。 replace 是替代它的符合 ember 的方法。 CollectionView 依赖于 replace 支持的数组突变观察器来了解要添加和删除哪些 View 。

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

关于ember.js - 动态删除条目后, Handlebars 模板不会更新以反射(reflect)计算属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15560991/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com