Get current URL in emberjs


#1

How can i get current URL in emberjs from afterModel? I tried using window.location.href but it returns url of the previous route. Also i’m using location: 'history'. Any ideas what could be the problem?


Way to refer back to url that launches Ember app
#2

To get the current url from a route instance, you can call this.get('router.url'). The reason you are getting the previous route’s URL is because of Ember’s run loop. Getting the url in afterModel resolves before the transition completes, giving you the route you were in before. To solve this you can wrap the call in an Ember.run.next:

afterModel: function (model){
    _this = this;

    Ember.run.next(function(){
        console.log _this.get('router.url')
    });
}
		

#3

That’s awesome. Thanks :slight_smile:


#4

How about transition.targetName

afterModel: function(model, transition) {
  console.log(Ember.get(transition, 'targetName'));
}

#5

@manoit88 That is better. Using transition promise, i can easily get url after the transition is complete. Thanks :slight_smile:

transition.then ->
  url = window.location.href

How to get current URL in a template that's inside a component?
#6

Do you know where I may find router.url in the API?


#7

#8

Thanks. How did you find it?


#9

I’ve spent some time in that module in the past fixing a bug.