4

i'm trying to migrate to the new Router in Ember. the use case is this: user is not logged in but requests a URL that requires login. he is redirected to a login route, after successful login he is redirected to his original destination.

i achieved this with the prior Router by overriding Router.route(path) and intercepting path requests when the app was in unauthorized state.

the new Router doesn't have a route() function, also, i don't know how to override it now that the Router instance is created automatically by Ember. i probably shouldn't do that anyway.

there is a Route.redirect() hook that looks useful. however, Route no longer extends Path in the v2 Router, so there is no Root.path, and no path information is passed into Route.redirect(), so i don't know how to save the path info for calling transitionTo() later.

i've supplied my general approach below. how can i accomplish this? it seems like a very common use case for many application.

  // i imagine something like this should happen
  App.AuthRequiredRoute = Ember.Route.extend({
       redirect: function() {
            if(!App.controllerFor('login').get('isLoggedIn')) {
                  var pathToSave = ????
                  App.controllerFor('login').set('pathAfterLogin',pathToSave);
                  this.transitionTo('login');
             }
        }
   }
   // and then after login, the LoginController would call App.router.transitionTo(this.pathAfterLogin)
4

1 に答える 1

0

私はこの 1 日か 2 日で、これについて自分で多くの調査を行いました。私が発見したことと、いくつかの考えをあなたと共有できます。

まず、現在のパスとコンテキストに関する情報を次のように取得できます。

this.router.router.currentHandlerInfos

これは配列を返します。各オブジェクトには、名前とコンテキスト プロパティの両方があります。これらの名前は、transitionTo で呼び出すルーターの名前に対応しています。

私の意見では、このようなもので作業することはできますが、面倒です。API ドキュメントではこれについて言及されておらず、これをパブリック API として使用する意図はない可能性があります。

より深くネストされた動的セグメントについても、上記のソリューションに問題があります。ルーター v2 がいかに新しいかを考えると、今後も進化し続け、より良いソリューションが現れる可能性が高いと思います。現在の場所を保存して、後日そこに戻りたいというのはかなり一般的なことです。

当面は、ApplicationController で認証済みフラグが設定されていない場合、リダイレクトではなく、アウトレットではなくログインを提示する条件付きブロックをテンプレートで使用しますか? 私はそれが「正しい」ものではないことを知っていますが、「今」です。

于 2013-01-08T23:05:48.940 に答える