# didInsertElement on new elements

**URL:** https://discuss.emberjs.com/t/didinsertelement-on-new-elements/7842
**Category:** Uncategorized
**Created:** [April 22, 2015, 2:53pm UTC](https://discuss.emberjs.com/t/didinsertelement-on-new-elements/7842 "2015-04-22T14:53:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![alfv89](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/alfv89/32/10637_2.png) [@alfv89](https://discuss.emberjs.com/u/alfv89)
#### Post date: [April 22, 2015, 2:53pm UTC](https://discuss.emberjs.com/t/didinsertelement-on-new-elements/7842/1 "2015-04-22T14:53:35Z")

</div>

I have a topic list that receive a bootstrap popover event on page load.

But when I add new topics to this list without reload page I can’t bind popover event in this new topic.

My code is organized in this way:

Forum route:

```
export default Ember.Route.extend({
  model: function() {
    return Ember.RSVP.hash({
      run: this.store.find('run', this.modelFor('dialogo.admin.manager').get('id')),
      topics: this.store.find('topic', { run: this.modelFor('dialogo.admin.manager').get('id') })
    });
  },
  setupController: function (controller, model) {
    controller.set('runID', this.modelFor('dialogo.admin.manager').get('id'));
    controller.set('model', model.topics);
    controller.set('currentRun', model.run);
  }
});

```

Forum Controller:

```
export default Ember.Controller.extend({
  needs: ['application'],
  activeUser: Ember.computed.alias("controllers.application.model"),
  actions: {
    createTopic: function() {
      var
        store = this.get('store'),
        user = this.get('activeUser'),
        run = this.get('currentRun'),
        topic;

      topic = store.createRecord('topic', {
        content: '',
        run: run,
        moderator: user
      });

      this.set('newTopic', topic);
    },
    saveNewTopic: function() {
      var newTopic = this.get('newTopic');
      var topicList = this.get('model');

      newTopic.save().then(function(topic){
        topicList.pushObject(topic);
        Ember.$('#newTopic').modal('hide');
      });
    },
    deleteTopic: function (topic) {
      topic.destroyRecord();
    }
  }
});

```

Forum View:

```
export default Ember.View.extend({
  didInsertElement: function() {
    this.$('[data-toggle="popover"]').popover({
      html : true,
      content: function() {
        var delID = Ember.$(this).attr('data-del');
        return Ember.$('#alertDel' + delID).html();
      }
    });
  }
});

```

Forum Template:

```
  <tbody>
    {{#each topic in model}}
      <tr>
        <th scope="row">{{topic.id}}</th>
        <td>{{topic.content}}</td>
        <td>{{topic.posts.length}}</td>
        <td>0</td>
        <td>
          <button class="btn btn-default" type="button" data-toggle="popover" data-placement="top" data-trigger="focus" data-del="{{topic.id}}" title="ATENÇÃO: Deletar tópico">
            DEL
          </button> |
          <span>EDIT</span> |
          <span>PEND</span> |
          <span>MOD</span>
          <div id="alertDel{{topic.id}}" style="display: none;">
            Você deseja deletar o tópico #ID {{topic.id}}?
            Essa ação não poderá ser desfeita.
            <br><br>
            <button class="btn btn-danger" {{action 'deleteTopic' topic}}>Sim</button> |
            <button class="btn btn-default">Não</button>
          </div>
        </td>
      </tr>
    {{else}}
      <tr>
        <td rowspan="5">Nenhum tópico encontrado...</td>
      </tr>
    {{/each}}
  </tbody>

```

So, when I execute saveNewTopic and add this new topic to topicList, I lost my popover event.

I searched for an answer but found nothing…

Anyone have a clue about how can I bind my popover event on new topic instances?

Thanks in advance!

---

<div class="post-metadata">

### Author: ![Snake](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Snake](https://discuss.emberjs.com/u/Snake)
#### Post date: [April 22, 2015, 4:53pm UTC](https://discuss.emberjs.com/t/didinsertelement-on-new-elements/7842/2 "2015-04-22T16:53:52Z")

</div>

Its because your only calling popover on the initial load, any new items won’t have the popover unless you observe for changes to the topics array and invoke `.popover` on the new topics.

I’d suggest moving the `tr` into a component. so you end up with

```
{{#each topic in model}}
   {{topic-row model=topic}}
{{/each}}

```

Then in you `topic-row` component you would attach the popover event on didInsertElement using the afterRender queue.

```
Ember.run.scheduleOnce('afterRender', this, function(){
  this.$('[data-toggle="popover"]').popover({
    html : true,
    content: function() {
      var delID = Ember.$(this).attr('data-del');
      return Ember.$('#alertDel' + delID).html();
    }
  });
})

```

probably also worth calling `.popover('destroy')` when the component is destroyed via `willDestroyElement`.

or see this [http://jsbin.com/zopod/1/edit?html,css,js,output](http://jsbin.com/zopod/1/edit?html,css,js,output) you can use `selector: '.target'`

---

<div class="post-metadata">

### Author: ![alfv89](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/alfv89/32/10637_2.png) [@alfv89](https://discuss.emberjs.com/u/alfv89)
#### Post date: [April 22, 2015, 9:56pm UTC](https://discuss.emberjs.com/t/didinsertelement-on-new-elements/7842/3 "2015-04-22T21:56:49Z")

</div>

@Snake thks a lot, use components works like a charm! 🙂

---

<div class="post-metadata">

### Author: ![ferroariel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ferroariel/32/9347_2.png) [@ferroariel](https://discuss.emberjs.com/u/ferroariel)
#### Post date: [April 23, 2015, 12:02am UTC](https://discuss.emberjs.com/t/didinsertelement-on-new-elements/7842/4 "2015-04-23T00:02:38Z")

</div>

This is a common problem when using jQuery to enhance UI controls created at runtime. Great solution indeed
