2013-03-02 83 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