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

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

问题描述

我有一个控制器和一个组件.当组件被渲染时,它以这种方式传递:

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}}

其中 feature 是通过控制器传递给把手的参数,而 parentController 是控制器.

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

现在,在控制器本身中,有一个属性(一个数组).让我们称该数组为 requiredValues.

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')

但是,我需要从 modal-filter 组件中观察这个控制器属性.所以在 modal-filter 组件中,我会把什么作为观察者函数:

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(???)

推荐答案

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

如果您传入控制器,您可以只在 parentController 属性上观看一个项目,尽管我根本不推荐这样做.

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.

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

Item 属性 foo 在 requiredValues 项目之一上发生了变化

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天全站免登陆