gpt4 book ai didi

ember.js - Ember 组件发送 Action 到路由

转载 作者:行者123 更新时间:2023-12-03 06:41:56 24 4
gpt4 key购买 nike

我有两个组件,一个组件位于另一个组件上。我需要从子组件向主路由发送事件(两个组件在同一路由中使用)

enter image description here

请告诉我是否有任何标准方法可以做到这一点。

最佳答案

对于简短的回答,您可以使用 ember-route-action-helper插件。

<button {{action (route-action 'onButtonClick')}}>ClickToCallRouteAction</button>

Action 通讯有三种方式,

<强>1。旧式经典函数风格,即从上到下将函数名称作为字符串传递。在所有我们需要定义和提供相同功能的地方。使用 sendAction 来冒泡。并将方法气泡从 Controller 发送到路由层次结构。

不鼓励这样做。经典款式样本actions twiddle

<强>2。关闭操作使用 action 辅助传递函数而不仅仅是字符串。这样您就不需要到处定义它。 sample twiddle for closure actions风格

<强>3。 route-action-helper addon 您可以直接从任何地方调用路由操作,只需使用 route-action 帮助器包装函数即可。

Sample twiddle

Classic 风格和 Closure 风格的比较以及为什么 Closure 更可取?

  • 在经典风格中,您需要在每个级别定义操作,并使用 sendAction 触发每个级别的操作,直到完全脱离嵌套。
  • 您可以在关闭操作中返回值,但不能在经典操作中返回值。
  • 您可以在关闭操作中 curry 值,但不能在经典操作中 curry 值。
  • 如果未找到操作,关闭操作会立即失败。但设计的经典 Action ,只会在调用时懒惰地引发错误值(value)观。
  • 编码复杂性,例如谁将处理操作和执行业务逻辑?。
  • 在闭包中,您可以结合操作和 mut helper 来设置具有值的属性。 onclick=(action (mut title) value="titlevalue")
  • 在闭包中,您可以指定调用函数的目标对象。 (action 'save' target=session) 将查看 session 对象上的 actions 哈希值,而不是当前上下文。

关于此的一些有前途的文章,
- miguelcamba文章ember-closure-actions-in-depth
- emberigniter 文章 send-closure-actions-up-data-owner
- emberjs 博客 1.13 release article
- 造船厂- ember-best-practice-stop-bubbling-and-use-closure-actions
- 来自 Ember map 的博客 Why action helper?
- Alisdair McDiarmid 的博客 ember-closure-actions-have-return-values
- 来自 alexdiliberto 的博客 ember-closure-actions

关于ember.js - Ember 组件发送 Action 到路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42295604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com