3

I need to get current url from Ember as a "redirectTo" parameter. The parameter will be used to redirect users back to the same page after they login from other app. The url looks like

http://test.com/apps/#/tables/7

I've found this post to get currentPath/route from the application. It provides the current route name but I wonder how I can get the complete path including id from Ember. I have a nested route declaration looks like below.

App.Router.map(function() {
  this.resource('tables', function() {
    this.resource('table', { path: ':table_id' });
  });
});

Any suggestion is welcome. Thanks!

Community
  • 1
  • 1
wen
  • 681
  • 1
  • 7
  • 17

3 Answers3

3

Observe the currentPath in ApplicationController and update the current URL:

App.ApplicationController = Ember.Controller.extend({
  currentPathDidChange: function() {
    App.currentUrl = window.location.href;
  }.observes('currentPath')
});
Panagiotis Panagi
  • 9,927
  • 7
  • 55
  • 103
1

Can you just use window.location?

var url = window.location.href;
Brandon
  • 38,310
  • 8
  • 82
  • 87
  • I tried that but the url doesn't get updated correctly when users click on different table. I can write jquery to observe the url change but I wonder if Ember has a way to do it. – wen Jun 17 '13 at 18:19
  • 3
    In my experience, this also will fail while executing integration/acceptance tests. It will always return `/tests`. – Chris Peters Apr 23 '15 at 16:08
-1

Thanks @Panagiotis Panagi, it works.

Furthermore, currentUrl can only be accessed from application controller, so if you want to get it from other place, you can add this code to a certain controller you want to access it :

export default Ember.Controller.extend({
  needs: ['application'],
  currentPath: Ember.computed.alias('controllers.application.currentPath'),
    ...

}
Orthocenter
  • 9
  • 1
  • 2