Ember组件可以观察控制器属性吗? [英] Can an Ember component observe a controller property?

查看:77
本文介绍了Ember组件可以观察控制器属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器和组件。当组件被呈现时,它以这种方式传递:

  {{modal-filter feature = feature parentController = this。控制器}} 

其中feature是通过控制器传递给handlebars的参数,parentController是控制器。



现在,在控制器本身中,有一个属性(一个数组)。我们可以调用该数组requireValues。



现在,在控件/组件本身中,我们可以轻松设置:

  valueObserver:function(){
...
} .observes('requiredValues')

但是,我需要从模态过滤器组件观察该控制器属性。所以在模态过滤器组件中,我将把它作为观察器的函数:

  valueObserver:function(){
...
} .observes(???)


解决方案

你不应该做什么,但我会告诉你如何完整的



如果你传递控制器,你可以看一下>

<$ parentController p $ p> valueObserver:function(){
...
} .observes('parentController.requiredValues')
pre>

这将假定整个数组正在被替换,而不仅仅是添加或更改的项目。



添加或删除的项目



  valueObserver:function(){
...
} .observes(' parentController.requiredValues。[]')



项目属性foo在其中一个requiredValues上更改项目



  valueObserver:function(){
...
} .observes('parentController.requiredValues你们应该做什么

而不是传入控制器,只需传递属性,并观察该属性。

  {{modal -filter feature = feature property = someProperty}} 


propertyObserver:Ember.observer('property',function(){
...
})


I have a controller and a component. when the component is rendered, it is passed on in this manner:

{{modal-filter feature=feature parentController=this.controller}}

where feature is a param passed in via controller to handlebars, and parentController is the controller.

Now, in the controller itself, there is an property (an array). let's call that array requiredValues.

Now within a controller/component itself, we can easily set:

valueObserver : function(){
     ...
}.observes('requiredValues')

However, I need to observe this controller property from a the modal-filter component. So in the modal-filter component, what would I put as the observer function:

valueObserver : function(){
     ...
}.observes(???)

解决方案

What you shouldn't do, but I'll tell you how for completeness

If you're passing the controller in, you can just watch an item on the parentController property, though I wouldn't recommend this at all.

valueObserver : function(){
     ...
}.observes('parentController.requiredValues')

This would assume the entire array is being replaced, not just an item added, or changed.

Item Added or Removed

valueObserver : function(){
     ...
}.observes('parentController.requiredValues.[]')

Item Property foo changed on one of the requiredValues items

valueObserver : function(){
     ...
}.observes('parentController.requiredValues.@each.foo')

What you should do

Instead of passing in the controller, just pass in the property, and observe the property.

{{modal-filter feature=feature property=someProperty}}


propertyObserver : Ember.observer('property', function(){
     ...
})

这篇关于Ember组件可以观察控制器属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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