将Ember Component附加到未由Ember管理的DOM元素 [英] Appending Ember Component to a DOM element not managed by Ember
问题描述
我想将一个Ember组件 ComponentB
附加到由 didInsertElement $上的一些非Ember UI库生成的DOM元素c $ c>
ComponentA
,导致类似于
I would like to append an Ember Component ComponentB
to a DOM element which is generated by some non-Ember UI library on didInsertElement
of ComponentA
, resulting in something like
<div class='ember-view component-a'>
<div class='i-know-nothing-of-ember'>
<div class='ember-view component-b'></div>
</div>
</div>
我知道 appendTo(element)
方法,但它失败与断言
I am aware of appendTo(element)
method, however it fails with assertion
您不能追加到现有的Ember.View。考虑使用Ember.ContainerView。
You cannot append to an existing Ember.View. Consider using Ember.ContainerView instead.
我还尝试在组件上调用 createElement
B然后通过jQuery将其附加到DOM - 哪种作品,但是最终它失败了错误
I also tried calling createElement
on component B and then appending it to DOM via jQuery - which kind of works, however in the end it fails with error
无法设置属性'_elementInserted 'null null
Cannot set property '_elementInserted' of null
请参阅 http://emberjs.jsbin.com/cofebo/2/
正确的方式是什么?以上;如果可能的行为和其他行为应该像组件A模板生成的我不知道的任何东西
。
推荐答案
我建议使用容器来查找组件,并在需要时将其附加到任何位置。
I suggest using the container to lookup the component and append it anywhere whenever you need to.
方法1 - 在路线内检索容器
http://emberjs.jsbin.com/libipazavu/1/edit?html,js,output
js
App = Ember.Application.create();
App.IndexRoute = Em.Route.extend({
setupController:function(controller,model){
controller.set("container",this.container);
}
});
App.IndexView = Em.View.extend({
appendNonEmberUILibrary:function(){
callNonEmberUILibrary();
var componentB = this.get("controller.container").lookup("component:component-b");
componentB.appendTo(".non-ember-ui");
}.on("didInsertElement")
});
App.ComponentBComponent = Em.Component.extend({
layoutName:"components/component-b",
prop1:"test-option-1"
});
function callNonEmberUILibrary(){
$("body").append("<div class='non-ember-ui' style='border:1px solid;'>element from non-ember ui lib</div>");
}
hbs
<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
<h3>Adding Ember Component to non-Ember DOM Element using <u><i>container</i></u></h3>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="components/component-b">
<br/>
<div style="border:1px dashed #F5664D">
This is componentB -> {{prop1}}
</div>
<br/>
</script>
方法2 - 在初始化程序中检索容器
http://emberjs.jsbin。 com / hijedowacu / 1 / edit?html,js,output
js
App = Ember.Application.create();
App.initializer({
name:"main",
initialize:function(container,application){
application.register('main:compB', container.lookup("component:component-b"), { instantiate: false });
application.inject("controller:index","theComponentB","main:compB");
}
});
App.IndexView = Em.View.extend({
appendNonEmberUILibrary:function(){
callNonEmberUILibrary();
var componentB = this.get("controller.theComponentB");
componentB.appendTo(".non-ember-ui");
}.on("didInsertElement")
});
App.ComponentBComponent = Em.Component.extend({
layoutName:"components/component-b",
prop1:"test-option-1"
});
function callNonEmberUILibrary(){
$("body").append("<div class='non-ember-ui' style='border:1px solid;'>element from non-ember ui lib</div>");
}
hbs
<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
<h3>Adding Ember Component to non-Ember DOM Element using <u><i>container</i></u></h3>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="components/component-b">
<br/>
<div style="border:1px dashed #F5664D">
This is componentB -> {{prop1}}
</div>
<br/>
</script>
方法3 - 使用jquery重新分配添加的组件(从评论更新)
或者,您可以在模板中添加 component-b
不可见,并且在 didInsertElement
中,您可以使用jquery重新分配并显示任何需要的内容。
Alternatively you could just add the component-b
in the template as non visible and within didInsertElement
you could reallocate and display it wherever required using jquery.
示例 http://jsbin.com/maginovexa/1/edit?html,js,output
js
App.IndexView = Em.View.extend({
prop2:"prop-from-index-view",
appendNonEmberUILibrary:function(){
callNonEmberUILibrary();
//var componentB = this.get("controller.container").lookup("component:component-b");
//componentB.appendTo(".non-ember-ui");
var componentBDOM = this.get("componentB").$().detach();
$(".non-ember-ui").append(componentBDOM);
}.on("didInsertElement"),
click:function(){this.set("prop2",Date.now());}
});
App.ComponentBComponent = Em.Component.extend({
layoutName:"components/component-b",
prop1:"test-option-1"
});
function callNonEmberUILibrary(){
$(".inner").append("<div class='non-ember-ui' style='border:1px solid;'>element from non-ember ui lib</div>");
}
hbs
<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
<h3>Adding Ember Component to non-Ember DOM Element using <u><i>container</i></u></h3>
<div class='inner'></div>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
this is index (click here to change prop2 of component)
<div style="display:none">
{{component-b prop2=view.prop2 viewName="componentB"}}
</div>
</script>
....
这是一个完全正常的解决方案,用于重新分配ember受控元素根据Simon Jesenko(op)的要求,这是一个不在ember视图中的非止损元素。
This is a fully working solution for reallocating an ember controlled element to a non-ember element that is within an ember view, as requested by Simon Jesenko (the op).
这篇关于将Ember Component附加到未由Ember管理的DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!