2013-03-02 99 views
2

我有一個應用程序,我做了一些有條件的重定向,但希望能夠在用戶跳過一些圈之後將請求傳遞到它的原始位置。Ember.js RC1獲取路由名稱

我有這樣的事情(的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') 

回答

2

這似乎是工作......但我不知道這是否是一個合法的方式來獲得這個值。

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

JSFiddle Example

+0

[本頁]上最後一個例子的第22行(​​http://emberjs.com/guides/routing/redirection/)也給出了這個理論的可信度;在這裏他們訪問'templateName'。謝謝! – wmarbut 2013-03-03 03:50:44

+1

看起來它是一個內部屬性,所以要小心依靠它:https://github.com/emberjs/ember.js/commit/6e64bac6b53deae6a2263510c1bca7bcb88d31a4 – CraigTeegarden 2013-03-03 19:35:19

+0

很好研究先生!我會離開接受這個答案,但也許有人像@ sly7_7可以擺脫一些像規範的方法 – wmarbut 2013-03-03 20:17:16

4

你想currentPathapplicationController

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

然後在你的任何控制器可以從applicationController訪問currentPath,通過使用needs API(讀到它here )如下:

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

謝謝你的答案;這在控制器內運行良好,但不幸的是在路由上下文中未定義(至少在第一次加載時)。所以,一旦你已經在一個控制器中,這將工作,但在這一點上,當前的路徑不再是我想要的。這是我的新信息,所以謝謝! – wmarbut 2013-03-03 03:51:44