5

I'm trying to get the current url in emberJS but I cannot. I am doing this:

App.Route = Ember.Route.extend(Em.I18n.TranslateableProperties, {
    actions: {
        didTransition: function (transition) {
            this._super();
            console.log(window.location.href);
            console.log(this.get('router.url'));
        }
     }
});

I used to use didTransition because I need to know the current URL when all elements are load. For example: If I am in home(/home) page and navigate to contact page(/contact-page), I want to know the url '/contact-page'.

If I use window.location.href works, but not always. I thought that didTransition was called when everything else is finished, but not.

Why?

Thanks in advance

Agustin Herrera
  • 363
  • 5
  • 14

1 Answers1

5

didTransition doesn't actually have a transition argument as your code suggests.

Instead, you should use the afterModel method like so:

App.Route = Ember.Route.extend(Em.I18n.TranslateableProperties, {
    afterModel: function(resolvedModel, transition) {
        var _this = this;  // keep a reference for usage in then()
        transition.then(function() {
            console.log(_this.get('router.url'));
        )};
    }
});

Reference:

rog
  • 5,351
  • 5
  • 33
  • 40
  • I'd like to use this suggestion but `afterModel` doesn't fire after every transition. Any other ways to get this info? – lizlux Sep 21 '15 at 17:02