我有一个Ember应用程序,可以在表格中显示借用文章的列表。 一个表格单元格有一个选择助手,它可以“借用”或“返回”作为值。
我也有一个复选框,通过查询参数触发显示返回的项目。
当我将复选框设置为不显示返回的项目并将一个项目从“借用”设置为“返回”时,该文章将保持可见。
所以我要做的是重新加载'filteredResults'并结合状态更改。
我阅读了Ember.observer,但我不确定这是否正确。
import Ember from 'ember'; export default Ember.Controller.extend({ queryParams: ['showReturned'], showReturned: false, possibleStates: ['borrowed', 'returned'], filteredResults: Ember.computed('showReturned', 'model', function() { const articles = this.get('model'); if (this.get('showReturned')) { return articles; } else { return articles.filterBy('state', 'borrowed'); } }) });I have an Ember app that displays a list of borrowed articles in a table. One table cell has a select helper that has either 'borrowed' or 'returned' as value.
I also have a checkbox that triggers the showing of returned items via query Parameters.
When I set my checkbox to not show returned items and set one item from 'borrowed' to 'returned', the article will stay visible.
So what I will have to do is reload the 'filteredResults' with the state change incorporated.
I read about Ember.observer but I'm not sure it's the right thing to use for this.
import Ember from 'ember'; export default Ember.Controller.extend({ queryParams: ['showReturned'], showReturned: false, possibleStates: ['borrowed', 'returned'], filteredResults: Ember.computed('showReturned', 'model', function() { const articles = this.get('model'); if (this.get('showReturned')) { return articles; } else { return articles.filterBy('state', 'borrowed'); } }) });最满意答案
您需要使用model.@each.state在每个model上观察state属性model.@each.state 。
filteredResults: Ember.computed('showReturned', 'model.@each.state', function() { const articles = this.get('model'); if (this.get('showReturned')) { return articles; } else { return articles.filterBy('state', 'borrowed'); } })同样,如果您只想观察是否有任何文章被创建或销毁,您可以观看数组 - 'model.[]' 。
您希望Ember.computed ( doc ),因为您将结果作为属性使用。 如果在每次更改某个状态(例如自动保存模型)时都想执行某个操作,则可以使用Ember.observer ( doc )。
You need to watch the state attribute on each model with model.@each.state.
filteredResults: Ember.computed('showReturned', 'model.@each.state', function() { const articles = this.get('model'); if (this.get('showReturned')) { return articles; } else { return articles.filterBy('state', 'borrowed'); } })Along the same lines, if you wanted to just watch whether any articles were created or destroyed, you could watch the array - 'model.[]'.
You want Ember.computed (doc) because you are consuming the result as a property. You would use Ember.observer (doc) if there was an action you wanted to perform every time one of the states changed (such as automatically saving the model).
更多推荐
发布评论