Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ravenstine/57048e83261aa1215458b0d2d44ff677 to your computer and use it in GitHub Desktop.
Save Ravenstine/57048e83261aa1215458b0d2d44ff677 to your computer and use it in GitHub Desktop.
Ember.js: Detecting route changes from within a component

I had a case where I wanted components to know if the current route has changed so behavior within their scope could be triggered. This was very hard to do, but this is what I came up with and it seems to work pretty well:

  didInsertElement() {
    Ember.run.scheduleOnce('afterRender', this, function() {
      let router = this.container.lookup('router:main');
      router.addObserver('currentPath', function(){
        // ... do something here
      });
    });
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment