質問:
新しい Ember.js ルーターを使用してプログラムで新しいルートに移行するにはどうすればよいですか?
背景/コンテキスト
send
古い Ember.js ルーターを使用すると、ルーターのメソッドを使用してルート/状態間をプログラムで遷移できます。
//OLD Router Syntax
App = Ember.Application.create({
Router: Ember.Router.extend({
root: Ember.Route.extend({
aRoute: Ember.Route.extend({
route: '/',
moveElsewhere: Ember.Route.transitionTo('bRoute')
}),
bRoute: Ember.Route.extend({
route: '/someOtherLocation'
})
})
})
});
App.initialize();
プログラムによる移行:
App.get('router').send('moveElsewhere');
新しい Ember.js ルーター (以下) を考えると、どうすれば同じことを達成できるでしょうか?
//NEW Router Syntax
App.Router.map(function(match) {
match('/').to('aRoute');
match('/someOtherLocation').to('bRoute');
});
回避策 (悪い解決策?)
これは正しくありませんよね?:
window.location = window.location.href + "#/someOtherLocation";
新しいルーターで動作しないように見える解決策:
1)インスタンスでsend
メソッドを呼び出すApp.router
> App.router.send("moveElseWhere")
TypeError: Cannot call method 'send' of undefined
2) Route を明示的に宣言し、イベントを設定する
App.ARoute = Ember.Route.extend({
events: {
moveElseWhere: function(context){
this.transitionTo('bRoute');
}
}
);
App.UploadRoute.moveElseWhere()
TypeError: Object (subclass of Ember.Route) has no method 'moveElseWhere'
注: 執筆時点では、Ember.js ルーターのドキュメントはまだ古いルーターを参照していますが、Ember.js ルーター ガイドは新しいルーターを参照しています。