假设有一条路径具有在用户请求时更新其数据的功能(假设后端为同一个调用返回不同的数据,也许是股票数据,或者只是随机数)。EmberJS:刷新数据模型并不会更新关联的计算属性
export default Ember.Route.extend({
model() {
return this.get('store').findAll('foo');
},
actions: {
invalidateModel() {
this.refresh();
}
}
});
现在,直接使用此模型的组件将按预期更新其视图。
Model: {{#each model as |m|}}{{m.bar}}{{/each}}
<button {{action "refreshModel"}}>Refresh model</button>
但是,如果组件正在使用观察模型的计算属性,则更新不会执行。
模板
Model: {{#each computedModel as |m|}}{{m}}{{/each}}
<br>
<button {{action "refreshModel"}}>Refresh model</button>
组件
computedModel: Ember.computed('model', function() {
return this.get('model').map(function(m) {
return `Computed: ${m.data.bar}`;
});
}),
对于一个完整的摄制,你可以检查出:https://github.com/myartsev/ember-computed-properties-on-data-model
最新承诺是不工作的计算性能的情况下。
previous commit是当直接使用模型时,所有内容仍然正常工作时。
我错过了什么?
如果你还没有,你也应该加入余烬。 https://ember-community-slackin.herokuapp.com/:) – Subtletree
你试过这个吗?它不工作:(烬数据模型是一个类,而不是一个数组,至少不是直接的(它确实有类内的数组) – myartsev
你没有正确地发送行动到组件,你只是发送一个字符串,尝试'{{foo-component model = model invalidateModel =(action“invalidateModel”)}}' – Subtletree