EmberJS notifyPropertyChange不让它到观察者? [英] EmberJS notifyPropertyChange not making it to observer?

查看:90
本文介绍了EmberJS notifyPropertyChange不让它到观察者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组件,里面有一个子组件列表(在父组件中绘制一个yield):

I have a component which has, inside it, a list of child components (being drawn with a yield inside the parent component):

parent-component
  for item in items
    child-component item=item childProperty=parentProperty

在子组件内,我有一个观察者childProperty,正确触发parentProperty更改。问题是我想在一个没有实际改变的时候触发这个观察者。

Inside child-component, I have an observer on "childProperty", which correctly fires any time parentProperty changes. The problem is that I'd like to trigger that observer in a time when the property hasn't actually changed.

要做到这一点,在我的父组件中有:

to do this, in my parent-component, I have:

this.notifyPropertyChange('parentProperty')

由于某种原因,这并不适用于子组件。这是一个JS bin,显示:

For some reason, this isn't making it to the child component. Here's a JS bin showing:

http: //emberjs.jsbin.com/caxedatogazo/1/edit

虽然我很高兴再谈谈我的用例,我更多感兴趣的是JS bin是否应该工作,如果没有,为什么..

While I'm happy to talk through my use-case more, I'm more interested in whether the JS bin should work, and if not, why..

非常感谢任何帮助!

推荐答案

当您在控制器上调用notifyPropertyChange时,只有在控制器中注册的观察者才会被通知属性更改。

When you call notifyPropertyChange on the controller, only observers registered within the controller are notified of the property change.

您的情况下,观察者在组件控制器内,而不是调用notifyPropertyChange的父控制器。

In your case, the observer is within the component controller and not the parent controller from where the notifyPropertyChange is called.

有一个很奇怪的方法来确保组件控制器被通知的财产变动。这可以通过向组件添加以下方法来完成。

There is a hacky way to ensure that the component controller is notified of the property change. This can be done by adding the following method to the Component.

didInsertElement: function() {
  var controller = this.get('targetObject'); 
  controller.addObserver('foo', this, this.onDataChange);
},

我们正在做的是获取父控制器,注册观察者foo与父控制器。

What we are doing is, getting the parent controller, registering an observer for foo with the parent controller.

这是同样的emberjs小提琴: http://emberjs.jsbin.com/rajojufibesa/1/edit

Here is the emberjs fiddle for the same: http://emberjs.jsbin.com/rajojufibesa/1/edit

希望这有帮助!

这篇关于EmberJS notifyPropertyChange不让它到观察者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆