DidInsertElement from the controller?

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

Now I have javascript that I need to execute in the $( document ).ready()

I saw didInsertElement in Ember Views, but how to do it 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 that it is not a good practice to use Ember Views?

+5
source share
2 answers

the controller is not inserted (there is a view), so there was no didInsertElement.

If you need to run something 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 }); 
+7
source

A more realistic answer (Ember 2.18+), following the same principle as in amenthes user’s answer: he is no longer recommended to use the Ember prototype extensions. Instead, you can directly override the controller's init() method. Note that you need to call this._super(...arguments) to make sure that all Ember-related codes are executed:

 import Controller from '@ember/controller'; export default Controller.extend({ init() { this._super(...arguments); // Don't forget this! // Write your code here. } }); 
0
source

Source: https://habr.com/ru/post/1205551/


All Articles