简体   繁体   English

“didInsertElement”的 Ember Octane 替代品是什么?

[英]What is the Ember Octane replacement for `didInsertElement`?

I'm trying to fire a callback passed down from a parent component.我正在尝试触发从父组件传递的回调。 Our old pattern for handling this was to call the function in didInsertElement.我们处理这个问题的旧模式是调用 didInsertElement 中的函数。 In Octane, I see that we can use the did-insert modifier but that seems weird for this use case since we're not updating the DOM element that we'd use to call did-insert.在 Octane 中,我看到我们可以使用did-insert修饰符,但这对于这个用例来说似乎很奇怪,因为我们没有更新我们用来调用 did-insert 的 DOM 元素。 I've also seen onRender being used in a few cases but I don't see documentation on that and it's not firing for me.我还看到onRender在一些情况下被使用,但我没有看到关于它的文档,它并没有为我开火。 Any suggestions?有什么建议?

For this specific use case, we have a parent component that can have one of many child components.对于这个特定的用例,我们有一个父组件,它可以有许多子组件之一。 And for each child component we have specific text that gets displayed in the parent component and we want the child component to be the owner of that text.对于每个子组件,我们都有特定的文本显示在父组件中,我们希望子组件成为该文本的所有者。

Your instinct that did-insert isn't the right solution here is, I think, correct.我认为,您认为did-insert不是正确解决方案的直觉是正确的。 In general, modifiers should only be used when the element they're going to be attached to is used in some way—that is, for managing interactions with the DOM.一般来说,修饰符应该只在它们将要附加到的元素以某种方式使用时使用——也就是说,用于管理与 DOM 的交互。 In general, we prefer one-way data flow otherwise.一般来说,我们更喜欢单向数据流。 However, the scenario you've outlined looks similar to a "registration" pattern, where when a child is instantiated, it tells its parent "Hey, I'm here, here's the info you need about me."但是,您概述的场景看起来类似于“注册”模式,其中当一个孩子被实例化时,它告诉它的父母“嘿,我在这里,这是您需要的关于我的信息。”

There are a couple reasonable options in this case:在这种情况下有几个合理的选择:

  1. Rethink whether the child component should in fact own that data.重新考虑子组件实际上是否应该拥有该数据。 I don't doubt you have a good reason for the child component owning the data, but the fact that the parent is already responsible for deciding which child to render may suggest that a mapping which includes both the component to render and the associated text could be a good solution in this space.我不怀疑您有充分的理由让子组件拥有数据,但父组件已经负责决定渲染哪个子组件这一事实可能表明,包含要渲染的组件和相关文本的映射可能成为这个领域的一个很好的解决方案。 That would cleanly solve this issue.那将干净利落地解决这个问题。

  2. Given that per your description you currently do want to avoid having the parent own that data, you could also consider having the child yield the data.鉴于根据您的描述,您目前确实希望避免让父母拥有该数据,您还可以考虑让孩子yield数据。 However, this usually only works if the DOM relationship for it makes sense.然而,这通常只有在它的 DOM 关系有意义时才有效。 If it does, you could choose to do something like this:如果是这样,您可以选择执行以下操作:

     {{yield (hash block='data' text=this.theText)}} <div class='the-component-body'> {{yield}} </div>
     <ChildComponent as |child|> {{#if (eq child.block 'data'}} <h2>{{child.text}}</h2> {{/if}} {{child}} </Child>

    (You can see this strategy working here —in particular, see the resulting DOM!) (你可以在这里看到这个策略的工作——特别是,看到生成的 DOM!)

    While that's very powerful, again, it only works if your DOM layout supports it.虽然这非常强大,但它只有您的 DOM 布局支持它时才有效。

  3. Finally, and perhaps most simply for the use case you have, you can (though not necessarily best , as I think the other options above are usually better when available), you can 'register' the value for the component by calling an action passed into your component during the constructor .最后,也许最简单的是对于您拥有的用例,您可以(虽然不一定是最好的,因为我认为上面的其他选项在可用时通常更好),您可以通过调用传递的操作来“注册”组件的值在constructor期间进入您的组件。 Now, using a component's constructor this way for component behavior can be a problem in that it only runs once, when the component is first instantiated, and Glimmer and Ember keep the component instance stable and just change the values passed to them over time as much as possible, so if the value you're passing back up depends on the argument you pass to it, the constructor won't work.现在,以这种方式使用组件的构造函数来处理组件行为可能是一个问题,因为它只运行一次,当组件第一次实例化时,Glimmer 和 Ember 保持组件实例稳定,并且随着时间的推移只更改传递给它们的值尽可能,所以如果你传递回来的值取决于你传递给它的参数, constructor将不起作用。 If it's always stable and does not depend on the arguments to a component, this does work, and it's often appropriate for a 'registration' pattern, where the child component simply needs to pass a single piece of data to the parent when instantiated.如果它总是稳定的,依赖于参数的组成部分,这样的工作,它通常适用于一个“注册”的格局,其中,子组件只需要实例化时,一个单件的数据传递给家长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM