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

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

问题描述

如果我有两个组件:

  • 导航菜单
  • 导航按钮

nav-menu 是一个块组件,它会包含 nav-button 像这样:

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

我希望按钮能够直接向菜单发送一个动作,告诉它切换其可见性状态.我想如果我连接到 Controller 中的互斥点,那么我可以执行以下操作:

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

这是唯一的方法吗?只是在寻找最优雅、以余烬为中心的方式来做到这一点.

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