Ember.computed完成后状态发生变化(State changes after Ember.computed finished)

编程入门 行业动态 更新时间:2024-10-18 10:36:43
Ember.computed完成后状态发生变化(State changes after Ember.computed finished)

我有一个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).

更多推荐

本文发布于:2023-08-02 18:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   发生   完成后   computed   Ember

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!