2

I have an ember application where I do some conditional redirecting, but would like to be able to pass the request on through to it's original location after the user has jumped through some hoops.

I've got something like this (coffeescript)

Ember.Route.reopen: ->
    redirect: ->
        if @controllerFor('specialOffers').get('should_offer')
            #This next line is what I need help with
            @controllerFor('specialOffers').set('pass_through', HOW_DO_I_GET_STRING_NAME_OF_CURRENT_ROUTE)
            # After this property is set and the user interacts
            # with the special offers, they will be redirected back
            # to wherever they intended to go
            @transitionTo('specialOffers')
wmarbut
  • 4,595
  • 7
  • 42
  • 72

2 Answers2

4

You want currentPath from applicationController:

App.ApplicationController = Ember.Controller.extend({
  printCurrentPath: function() {
    var currentPath = this.get('currentPath')
    console.log("The currentPath is " + currentPath);
  }.observes('currentPath')
}); 

Then in any of your controllers you can access the currentPath from the applicationController, by using the needs API (read about it here), as follows:

App.SomeOtherController = Ember.Controller.extend({
  needs: ['application'],

  printCurrentPath: function() {
    var applicationController = this.get('controllers.application');
    var currentPath = applicationController.get('currentPath');
    console.log('Look ma, I have access to the currentPath: ' + currentPath);
  }.observes('controllers.application.currentPath')
});
Panagiotis Panagi
  • 9,927
  • 7
  • 55
  • 103
  • Thanks for the answer; this works well within controllers, but unfortunately comes up as undefined in the context of the route (at least on the first load). So this will work, once you are already in a controller, but at that point the current path is no longer what I want. This is new info to me though, so thanks! – wmarbut Mar 03 '13 at 03:51
2

This seems to work... but I don't know if it is a legitimate way to get this value.

Ember.Route.reopen({
  redirect: function() {
    console.log(this.routeName);
  }
})

JSFiddle Example

CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
  • Line 22 of the last example on [this page](http://emberjs.com/guides/routing/redirection/) gives credence to this theory as well; Here they access `templateName`. thanks! – wmarbut Mar 03 '13 at 03:50
  • 1
    Looks like it is an internal property, so be careful relying on it: https://github.com/emberjs/ember.js/commit/6e64bac6b53deae6a2263510c1bca7bcb88d31a4 – CraigTeegarden Mar 03 '13 at 19:35
  • well researched sir! I'll leave the accept on this answer, but maybe someone like @sly7_7 could shed some like on the canonical approach – wmarbut Mar 03 '13 at 20:17