升级到 Ember-Data 2.14 破坏了 hasMany 上的计算过滤器
Upgrading to Ember-Data 2.14 broke computed filter on hasMany
我试过在旋转中重现这个问题,但不幸的是,旋转总是成功,但我希望这个错误对某些人来说听起来很熟悉,他们可以为我指明正确的方向。
这里是小玩意儿:
https://ember-twiddle.com/9ee6f28479449b7b1859e4c090490775?openFiles=routes.parent.js%2C
所有名称都已更改,以使我们的数据安全人员感觉更好:
基础知识:
我有一个模型 'parent' 与 'child'.
具有异步 'hasMany' 关系
children: hasMany('child', {async: true}),
然后我有一个计算的 属性 绑定到 hasMany:
sons: Ember.computed.filterBy('children', 'type', 'son'),
然后我为 firstObject 添加别名以获取 'sons' 数组中的第一项:
firstSon: Ember.computed.alias('sons.firstObject')
在parentroute.setupController中,我新建了一个'son'object:
setupController: function(controller, model) {
this._super(...arguments);
var toy = this.get('store').createRecord('toy', {
type: 'latte'
});
this.get('store').createRecord('child',
{parent: model, type: 'son', toy: toy,
name: 'Tyler'});
}
使用 Ember 2.14 和 Ember-Data 2.13,或匹配版本的任何较低组合,这有效,我的模板可以毫无问题地引用 parent.firstSon。
一旦我将 Ember-Data 升级到 2.14,当我到达我想要的路线时,parent.children 有正确的 child 但是 sons: filterBy 是空的,因此别名到sons.firstObject 也是空的。 (通过 Ember 检查员验证)
提醒:我链接的 twiddle 确实有效,它是我应用程序中更复杂的版本失败了。
简单的升级操作就破坏了它 - 没有发生其他代码更改。
我在 ember-data github 中看到了一些类似的问题:
这个问题:https://github.com/emberjs/data/issues/4974 类似,但它似乎从 2.12 开始,并且似乎与避免加载数据的新 'hasMany' 调用有关。我只是绑定到实际的异步 hasMany
https://github.com/emberjs/ember.js/issues/16258 引用了一个类似的问题,但仅以 Ember 3.0.0 开头并且在 2.18 中不存在 - 我的问题发生在 2.14.1 到 ~2.18
有没有想过什么东西可能坏了?
看起来这是惰性 hasMany 创建的另一个问题。我调用了我做的 afterModel:
afterModel: function(model) {
var parentPromise = this._super(...arguments);
var loadChildren = () => {
return model.get('children').then(children => {
this.set('children', children);
});
};
return (parentPromise && parentPromise.then) ? parentPromise.then(loadChildren) : loadChildren();
},
并将 setupController 更改为:
setupController: function(controller, model) {
this._super(controller, model);
var hasSons = model.get('sons').length > 0;
if(!hasSons) {
var toy = this.get('store').createRecord('toy', {...etc});
var source = this.get('store').createRecord('child',
{parent: model, type: 'son', toy: toy});
this.get('children').removeObject(son).pushObject(son);
}
},
现在我恢复了旧功能
注意:
this.get('children').removeObject(儿子).pushObject(儿子);
来自 https://github.com/emberjs/data/issues/4974 的推荐
我确实需要 removeObject 和 pushObject 才能让它工作
我试过在旋转中重现这个问题,但不幸的是,旋转总是成功,但我希望这个错误对某些人来说听起来很熟悉,他们可以为我指明正确的方向。
这里是小玩意儿: https://ember-twiddle.com/9ee6f28479449b7b1859e4c090490775?openFiles=routes.parent.js%2C
所有名称都已更改,以使我们的数据安全人员感觉更好:
基础知识: 我有一个模型 'parent' 与 'child'.
具有异步 'hasMany' 关系children: hasMany('child', {async: true}),
然后我有一个计算的 属性 绑定到 hasMany:
sons: Ember.computed.filterBy('children', 'type', 'son'),
然后我为 firstObject 添加别名以获取 'sons' 数组中的第一项:
firstSon: Ember.computed.alias('sons.firstObject')
在parentroute.setupController中,我新建了一个'son'object:
setupController: function(controller, model) {
this._super(...arguments);
var toy = this.get('store').createRecord('toy', {
type: 'latte'
});
this.get('store').createRecord('child',
{parent: model, type: 'son', toy: toy,
name: 'Tyler'});
}
使用 Ember 2.14 和 Ember-Data 2.13,或匹配版本的任何较低组合,这有效,我的模板可以毫无问题地引用 parent.firstSon。
一旦我将 Ember-Data 升级到 2.14,当我到达我想要的路线时,parent.children 有正确的 child 但是 sons: filterBy 是空的,因此别名到sons.firstObject 也是空的。 (通过 Ember 检查员验证)
提醒:我链接的 twiddle 确实有效,它是我应用程序中更复杂的版本失败了。
简单的升级操作就破坏了它 - 没有发生其他代码更改。
我在 ember-data github 中看到了一些类似的问题: 这个问题:https://github.com/emberjs/data/issues/4974 类似,但它似乎从 2.12 开始,并且似乎与避免加载数据的新 'hasMany' 调用有关。我只是绑定到实际的异步 hasMany
https://github.com/emberjs/ember.js/issues/16258 引用了一个类似的问题,但仅以 Ember 3.0.0 开头并且在 2.18 中不存在 - 我的问题发生在 2.14.1 到 ~2.18
有没有想过什么东西可能坏了?
看起来这是惰性 hasMany 创建的另一个问题。我调用了我做的 afterModel:
afterModel: function(model) {
var parentPromise = this._super(...arguments);
var loadChildren = () => {
return model.get('children').then(children => {
this.set('children', children);
});
};
return (parentPromise && parentPromise.then) ? parentPromise.then(loadChildren) : loadChildren();
},
并将 setupController 更改为:
setupController: function(controller, model) {
this._super(controller, model);
var hasSons = model.get('sons').length > 0;
if(!hasSons) {
var toy = this.get('store').createRecord('toy', {...etc});
var source = this.get('store').createRecord('child',
{parent: model, type: 'son', toy: toy});
this.get('children').removeObject(son).pushObject(son);
}
},
现在我恢复了旧功能 注意: this.get('children').removeObject(儿子).pushObject(儿子); 来自 https://github.com/emberjs/data/issues/4974 的推荐 我确实需要 removeObject 和 pushObject 才能让它工作