15

アカウントと account/:account_id オプションを使用してルーターをセットアップしました。ユーザーがアプリのインデックス ページに到達すると、アカウント ルートに移行します。

Social.Router.map(function() {
    this.resource('accounts', function(){
        this.resource('account', { path: ':account_id'});
    });
});

Social.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('accounts');
    }
});

私がやりたいことは、いくつかの基準に基づいて、指定された :account_id ルートに移行することです。現時点では、配列の最初のアカウントを取得してそれを使用したいだけです。しかし、将来的には、最後に表示していたアカウントに移行する方法になる可能性があります。このようなもの:

Social.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('accounts/:account_id');
    }
});

ドキュメントには「詳細」が記載されていますが、例は提供されておらず、次のもののみが提供されています。

transitionTo (名前、モデル)

別ルートに乗り換え。必要に応じて、問題のルートのモデルを提供します。モデルは、シリアライズ フックを使用して URL にシリアライズされます。

私は次のことを試しました:

this.transitionTo('accounts/4');
Uncaught Error: assertion failed: The route accounts/4 was not found

this.transitionTo('accounts', Social.Account.find(1));
Uncaught More objects were passed than dynamic segments
4

5 に答える 5

13

私は他の人の答えといくつかのいじりをまとめて、この答えを出しました:

次のようにルートを定義します。

this.resource('accounts', function () {
    this.route('account', {path: '/:account_id'});
});

リダイレクト:

this.transitionTo('accounts.account', accountObj);

But if you are loading from server, you need the accountObj object loaded before redirect:

var accountObj = App.Account.find(1);
accountObj.one('didLoad', this, function () {
    this.transitionTo('accounts.account', accountObj);
});

I set up this fiddle with complete example

于 2013-02-28T11:05:01.897 に答える
3

transitionTo は、transitionToRoute を優先して廃止されたようです。

それにもかかわらず、元の宣言をすることで再ルーティングを実現でき、this.resource('account', { path: '/:account_id'});作成された単一のオブジェクトでの遷移が機能するはずです。

于 2013-02-27T07:04:05.257 に答える
2

ルート パスを適切に指定していないため、別のリソースではなく、リソースの下にルートが必要です。次のようになります。

Social.Router.map(function() {
    this.resource('accounts', function(){
        this.route('account', { path: '/:account_id'});
    });
});

Social.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('accounts.account', Social.Account.find(1));
    }
});
于 2013-02-27T20:59:39.997 に答える
1

最新の Ember (1.0.0-RC-6) では、これは完全に機能します。

ルーター:

this.resource('accounts', function () {
    this.resource('account', {path: '/:account_id'});
});

リダイレクトするには:

this.transitionToRoute('account', Social.Account.find(1))
于 2013-07-24T07:04:01.153 に答える
0

/:account_id はリソースであるため、transitionToRoute 'account' が必要です。それに応じて AccountRoute も必要です。

/:account_id がリソースではなくルートである場合は、transitionToRoute 'accounts.account' となり、ルート ハンドラは AccountsAccountRoute と呼ばれます。

于 2013-02-27T21:54:04.350 に答える