in3xu4
in3xu4

Reputation: 978

didInsertElement in Ember 2.0+

Ember.View is deprecated, in favor of Components. That's great but I'm having trouble making sense of the 2.0 release.

Most often, I used the didInsertElement hook to run some jQuery code etc. But now that the Em.View class has been deprecated, how can I achieve the same thing? I don't want to create a component or anything like that. It doesn't make sense to create components for normal pages(routes). Simply because its not a re-usable thing plus component's scopes are isolated.

Say we have a about route, and when the template is rendered I just want to sun some jQuery code. How can I do this in Ember 2.0+?

Upvotes: 2

Views: 737

Answers (2)

NicholasJohn16
NicholasJohn16

Reputation: 2409

While it doesn't seem like the best approach, creating a component is probably your best option. Soon we'll have routeable components which will take over much of what controllers and views use to do. Creating a component and just inserting it into your template should put you on a good path to be ready for routeable components.

Upvotes: 2

Daniel
Daniel

Reputation: 18692

You could take advantage of didTransition hook and Ember.run.next. Check my solution:

export default Ember.Route.extend({
  actions: {
    didTransition() {
      Ember.run.next(this, 'initParticles');
    }
  },
  initParticles() {
    let ammount = (window.matchMedia('(max-width: 456px)').matches) ? 40 : 100;
    particlesJS('particles-js', {
      // my options
    });
  }
});

Upvotes: 2

Related Questions