【问题标题】:Creating a component from a plugin's element when plugin is already inside another component当插件已经在另一个组件中时,从插件的元素创建一个组件
【发布时间】:2015-12-10 16:23:07
【问题描述】:

我正在使用第三方插件,我无法控制它。除了这个插件之外,我在整个应用程序中都使用 Ember,我需要在由所述插件呈现的 div 内实例化一个组件,以按照我想要的方式控制它(避免使用 jQuery 事件等)。

1- 我正在创建一个名为“MyNewComponent”的组件

2- 当前结构:

<myComponent1>
    <myPlugin-notEmber>
       <div-where-I-want-to-append-MyNewComponent class="divClass"/>
    </myPlugin-notEmber>
</myComponent1>

3- 考虑到“div-where-I-want-to-append-MyNewComponent”是由插件呈现的,而不是由我呈现的。

4- 我目前在 myComponent1 中尝试做的是:

onDidInsertElement: Em.on('didInsertElement', function() {
    this.$().find('.divClass').each(function(index, element) {
        MyNewComponent.create().appendTo(Em.$(element));
    });
}),

为什么它不起作用:

我得到了这个:(Ember 1.13)

“您不能附加到现有的 Ember.View。考虑改用 Ember.ContainerView。”

我在寻找什么:

a) 正确的方法或

b) 等效的替代方案(将在该插件中创建一个组件)

【问题讨论】:

    标签: ember.js ember-cli


    【解决方案1】:

    您最好的选择是使用ember-wormhole

    这允许您将任何内容放在另一个带有 id 的 div 中,所以:

    {{#ember-wormhole to="destination"}}
      {{my-new-component}}
    {{/ember-wormhole}}
    

    还有其他你会拥有的地方

    <div id="destination"></div>
    

    这将在该 div 中呈现 {{my-new-component}}

    【讨论】:

    • 抱歉回复晚了。是的,它确实奏效了。 Ember 虫洞非常适合解决此类问题。它还保留所有绑定。
    猜你喜欢
    • 2016-08-26
    • 2021-09-27
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 2017-04-28
    • 2022-11-27
    相关资源
    最近更新 更多