Ember.js RC1 get name of route

2019-04-14 19:21发布

问题:

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')

回答1:

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



回答2:

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')
});