【发布时间】:2018-02-22 23:09:22
【问题描述】:
假设有一条路由可以在用户请求时更新其数据(假设后端为同一个调用返回不同的数据,可能是股票数据,或者只是随机数)。
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 表示在直接使用模型时一切正常。
我错过了什么?
【问题讨论】:
标签: ember.js