Search code examples
ember.jsember-cli

didInsertElement from controller?


I have an ember application with a controller header.js and a template header.hbs.

Now I have some javascript I need to execute at document $( document ).ready()

I saw on Ember Views there is didInsertElement but how do I do this from the controller?

// controllers/header.js
import Ember from 'ember';
export default Ember.Controller.extend({
});

// views/header.js
import Ember from 'ember';
export default Ember.Controller.extend({
});

// templates/header.js
test

I read several times it's not good practice to be using Ember Views?


Solution

  • the controller is not inserted (the view is) hence there is no didInsertElement.

    If you need something to run once, you can write something like this:

    import Ember from 'ember';
    export default Ember.Controller.extend({
        someName: function () {  // <--- this is just some random name
            // do your stuff here
        }.on('init')  // <---- this is the important part
    });