2

我有一个 ember 应用程序,我在其中进行一些条件重定向,但希望能够在用户跳过一些障碍后将请求传递到它的原始位置。

我有这样的东西(咖啡脚本)

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

2 回答 2

4

你想currentPathapplicationController

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

然后在您的任何控制器中,您可以使用API(在此处currentPath阅读)从中访问,如下所示:applicationControllerneeds

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')
});
于 2013-03-02T23:01:31.783 回答
2

这似乎可行......但我不知道这是否是获得这个价值的合法方式。

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

JSFiddle 示例

于 2013-03-02T22:41:14.343 回答