Search code examples
ember.jsember-router

Single callback in Router, to access previous/current url + Route information?


I've been looking around on Google and Stack Overflow for an answer to this: with Ember-cli 2.10, how can I set up a single callback in the Router, which gives me information about the previous and current URL, as well as the name of the Route about to be called? I'd like to pass all 3 of those to an analytics platform.

Every example I've found has either depended on deprecated Ember features, or just plain hasn't worked as expected. Love to hear an answer on this. Also happy to hear what a better design might be, given the above analytics requirements.


Solution

  • All your requirements will be covered in Public Router Service pull request. It includes properties currentRouteName, currentURL, location, rootURL and transitionTo method. It's in canary build, not yet production ready. to play you need to enable this feature config/environment.js

    "FEATURES": {
          "ember-routing-router-service": true
        }
    

    You can just inject router service anywhere and get the properties.

    router: Ember.inject.service(),
    

    Twiddle Copied From Miguel Camba