Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending Ember Component to a DOM element not managed by Ember

Tags:

ember.js

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>

I am aware of appendTo(element) method, however it fails with assertion

You cannot append to an existing Ember.View. Consider using Ember.ContainerView instead.

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

Cannot set property '_elementInserted' of null

See http://emberjs.jsbin.com/cofebo/2/

What is the proper way to achieve the above; if possible actions and other behaviours should be as if i-know-nothing-of-ember would be generated by Component A template.

like image 901
jesenko Avatar asked Jan 27 '15 08:01

jesenko


1 Answers

I suggest using the container to lookup the component and append it anywhere whenever you need to.

Approach 1 - retrieve container within route

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>

Approach 2 - retrieve container within an initializer

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>

Approach3 - reallocate added component with jquery (update from comments)

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.

example 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>
....

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).

like image 69
melc Avatar answered Oct 16 '22 18:10

melc