发送操作以包含组件 [英] Sending an action to containing component

查看:88
本文介绍了发送操作以包含组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个组件:


  • 导航菜单

  • 导航按钮

  • nav-menu
  • nav-button

nav-menu 是一个块组件,将包含 导航按钮像这样:

and nav-menu is a block component that would contain nav-button like so:

{{#nav-menu}}
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    {{nav-button}}
{{/nav-menu}}

我希望按钮可以直接发送一个动作到菜单,告诉它切换它的可见性状态。我猜,如果我在控制器中插入一个mutux点,那么我可以做一些例子:

I'd like the button to be able to directly send an action to the menu to tell it to toggle it's visibility state. I guess if I hook into a mutux point in Controller then I could do something like:

{{#nav-menu toggleNavigation=mutex}}
    {{nav-button action=mutex}}
{{/nav-menu}}

这是唯一的办法吗?只需寻找最优雅,以ember为中心的方式。

Is this the only way? Just looking for the most graceful, ember-centric way of doing this.

推荐答案

我以前遇到过这个问题,不幸的是公共API没有办法这样做。创建块组件时,其内部呈现的内容具有外部范围的上下文,组件。不幸的是,没有办法改变这种行为,但这真的是有道理的。

I've run into this issue before and unfortunately, there's no way in the public API to do this. When creating a block component, anything rendered inside of it has the context of the outer scope, not the component. It's unfortunate that there's no way to change this behavior, but it really does make sense.

我会说,你提出的方式是处理这个问题的最好方法情况:控制器上有一个传递给外部组件的变量。它符合Ember的数据下降,行动的理念。

I would say that the way you've proposed is the best way to handle this situation: have a variable on the controller that's passed to the outer component. It's inline with Ember's "data down, actions up" philosophy.

这篇关于发送操作以包含组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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