简体   繁体   中英

Ember.js RC1 get name of route

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM