Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to get location during route's hook gives you the previous location #10906

Closed
mgenev opened this issue Apr 19, 2015 · 6 comments
Closed

Comments

@mgenev
Copy link

mgenev commented Apr 19, 2015

When I try to get my url with window.location object in an Ember route's hook, I actually get the previous location. I dug around and this works

afterModel: function (model){

   Ember.run.schedule('afterRender', () => {
        console.log( this.get('router.url'))
    });
}

but only sometimes so it may be a race condition, in which case it sometimes updates on time and sometimes it doesn't. Only when I schedule it from the afterModel hook, it works consistently so far.

The didTransition event in particular should have the accurate full url at least.

Either way it's a problem that it is not at all obvious from documentation or anywhere that this is doable. So over 1 hour down the drain to get the url - something which the browser gives you readily and every developer knows how to do in 1 second... unless your framework obscures it and its route hooks are out of sync with the browser...

@runspired
Copy link
Contributor

a HUGE +1

This jsBin helps to show the issue: http://jsbin.com/hizacuyobi#/posts

I think the following hooks / events should have access to the new url.

  • enter
  • activate
  • setupController
  • renderTemplate
  • didTransition

Needing to call Ember.run.schedule('afterRender') from within just the right hook to get the new url is janky, but if that's the intended behavior it should also be well documented because history tracking / analytics needs both need access to a new url and are common use cases.

In my own app, I did this to get around the issue

var Router = Ember.Router.extend({

  location: config.locationType,

  urlHistory : [],

  /*
    Provide the ability to retrieve urls from history
    by adding new urls to urlHistory when the route
    changes
   */
  updateUrlHistory: function() {
    Ember.run.schedule('afterRender', this, function() {
      var location = this.get('_location') || this.get('location'),
        url = location.lastSetURL || this.get('url');
      //this.get('urlHistory').pushObject({ url : url});
    });
  }.observes('_location', 'url', 'location')

});

I completely understand that until enter the transition can still bail, and it doesn't make sense for the new url to be present yet. After enter though it does, and especially during didTransition.

If the intent is to delay updating the url until the onscreen content has/is rendering, I would suggest providing an explicit and documented means of accessing the new url from within hooks and the didTransition event.

@ryanlitalien
Copy link

@runspired Thanks for your workaround. I used the afterRender in my application didTransition hook:

Route = Ember.Route.extend
  actions:
    didTransition: ->
      Ember.run.schedule 'afterRender', @, ->
        @formatTitle()

  formatTitle: ->
    urlSplit = window.location.href.split("/")
    ...

@acorncom
Copy link
Contributor

Tagging this as something to be re-checked against the latest canary

@pixelhandler
Copy link
Contributor

per @runspired suggestion:

If the intent is to delay updating the url until the onscreen content has/is rendering, I would suggest providing an explicit and documented means of accessing the new url from within hooks and the didTransition event.

I think this is a documentation issue to surface when the transition has made it to a new URL. cc/ @locks @acorncom

@scalvert
Copy link
Contributor

This issue came up during my implementation of the router service RFC. I identified the issue (it was in router:main), and fixed it in #14805. I also added tests to ensure this is working.

@btecu
Copy link
Contributor

btecu commented Apr 26, 2018

This issue can be closed.
cc @rwjblue

@rwjblue rwjblue closed this as completed Apr 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants