m0meni
m0meni

Reputation: 16445

Ember.js run function on route change for every single route. Global activate function?

I'm using the MathJax library which lets you have latex on the page, but it only runs if you visit the route from a <a>. If you visit from a {{link-to}} helper, it won't run it's typesetting function because the page isn't actually reloading.

I've been able to work around this by putting this in every route:

  activate () {
    Ember.run.scheduleOnce('afterRender', this, function() {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.body]);
    });
  }

but I'm curious if there's a global activate function I can overwrite/extend instead to avoid having to put this statement on every single page.

Upvotes: 2

Views: 355

Answers (1)

Gennady Dogaev
Gennady Dogaev

Reputation: 5991

You may create a mixin (http://emberjs.com/api/classes/Ember.Mixin.html):

//app/mixins/base-route.js
import Ember from 'ember';

export default Ember.Mixin.create({
  activate () {
    Ember.run.scheduleOnce('afterRender', this, function() {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.body]);
    });
  }
});

//app/routes/some.js
import Ember from 'ember';
import RouteMixin from '../mixins/base-route';

export default Ember.Route.extend(RouteMixin, {});

Or try to use reopen (http://guides.emberjs.com/v1.13.0/object-model/reopening-classes-and-instances/)

Upvotes: 4

Related Questions