Ember.js:模型的计算属性,不在组件的变更引用中呈现

时间:2021-10-03 21:10:18

The issue I am experiencing is quite clearly expressed in this jsbin, where the computed property isFinito does not change in the template along with the value of isFinished. Is this a feature of components, that they can't listen to computed properties of models passed to them ? If so, how can I achieve this. It seems like a very common need but no matter how much I searched about this, I couldn't find something that duplicates this problem or solves it.

我遇到的问题在这个jsbin中非常清楚地表达出来,其中计算属性isFinito在模板中不会随着isFinished的值而改变。这是组件的一个特性,它们无法监听传递给它们的模型的计算属性吗?如果是这样,我怎样才能做到这一点。这似乎是一个非常普遍的需求,但无论我多少搜索这个,我找不到复制这个问题或解决它的东西。

1 个解决方案

#1


1  

This actually has nothing to do with observers - the cause is property access. Consider the following line:

这实际上与观察者无关 - 原因是属性访问。请考虑以下行:

return (this.isFinished ? "SI" :"NON");

This is always going to return NON because this.isFinished is always falsy (it's undefined). Ember-Data doesn't put data directly on the model instances, it just puts computed properties there. this.isFinished doesn't exist. If you use Ember's get() method, it works fine:

这总是会返回NON因为this.isFinished总是假的(它是未定义的)。 Ember-Data不会将数据直接放在模型实例上,只是将计算属性放在那里。 this.isFinished不存在。如果你使用Ember的get()方法,它可以正常工作:

return (this.get('isFinished') ? "SI" :"NON");

As a general rule, always use get() when dealing with Ember objects. I know it's a little annoying at first, but you get used to it and it helps avoid a lot of issues.

作为一般规则,在处理Ember对象时始终使用get()。我知道一开始有点讨厌,但你已经习惯了它,它有助于避免很多问题。

#1


1  

This actually has nothing to do with observers - the cause is property access. Consider the following line:

这实际上与观察者无关 - 原因是属性访问。请考虑以下行:

return (this.isFinished ? "SI" :"NON");

This is always going to return NON because this.isFinished is always falsy (it's undefined). Ember-Data doesn't put data directly on the model instances, it just puts computed properties there. this.isFinished doesn't exist. If you use Ember's get() method, it works fine:

这总是会返回NON因为this.isFinished总是假的(它是未定义的)。 Ember-Data不会将数据直接放在模型实例上,只是将计算属性放在那里。 this.isFinished不存在。如果你使用Ember的get()方法,它可以正常工作:

return (this.get('isFinished') ? "SI" :"NON");

As a general rule, always use get() when dealing with Ember objects. I know it's a little annoying at first, but you get used to it and it helps avoid a lot of issues.

作为一般规则,在处理Ember对象时始终使用get()。我知道一开始有点讨厌,但你已经习惯了它,它有助于避免很多问题。