问题描述
我已经创建了一个模拟应用程序来说明我所面临的情况:
I have created a mock application to illustrate the situation I am facing: Mock App
在此应用程序中;我创建了一个带有一个布尔属性的服务,并且一个函数来切换该属性( x
);和两个组件(一个用于切换服务的属性;另一个用于观察切换的数量并显示它)。观察者位于 toggle-observer
中。它直接添加到服务的属性: myService.x
。代码不工作;但是如果 toggle-observer.js
的第14行的注释被注释掉;观察者开始工作。
In this application; I have created a service with a single boolean property and a function to toggle that property (x
); and two components (one to toggle the service's property; the other to observe the number of toggles and display it). The observer lies in toggle-observer
. It is added directly to service's property as: myService.x
. The code is not working as is; however if the comment at line 14 of toggle-observer.js
is commented out; the observer starts working.
我的问题是,我需要执行 get
到整个路径一个嵌套的观察者属性让它工作?这是预期的行为吗?如果是这样,有人可以解释为什么吗?我最好的问候。
My question is that, do I need to perform a get
to the whole path of a nested observer property to get it working? Is this the expected behavior? If so, can somebody explain why? My best regards.
注意:这是一个模拟示例来说明情况;它与我在真实应用程序中设计的任何东西无关。我尽量避免观察员;但是在尝试sth时遇到这种情况。并决定要求它。
Note: This is a mock example to illustrate the case; it is not related to anything I am designing in a real app. I am trying to avoid observers as much as possible; but I ran into this situation while trying out sth. and decided to ask it.
推荐答案
从ember指南
从ember指南,。
通过结合上述两个概念,我们可以得出以下结论,
From ember guide, unconsumed computed properties do not trigger observers. By combining the above two concepts, we can come to the below conclusion,
您没有使用 myService
toggle-observer
组件中的任何属性,所以它将是 undefined
直到你明确地调用 get
函数或在模板中使用它。
You haven't used myService
any of the property inside toggle-observer
component so it will be undefined
until you explicitly call get
function or use it in template.
除非你使用它 x
属性在 toggle-observer
组件中,则不会触发观察者。您需要在toggle-observer.hbs文件或 init
方法中使用它。
Unless you use it x
property in toggle-observer
component, then it will not trigger observer. You need to consume it either in toggle-observer.hbs file or in init
method.
这篇关于Ember观察者无法处理嵌套的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!