新しいEmberルーターでモーダル状態/ビューを処理する正しい方法がわかりません。より一般的には、「メイン」状態(URL)に影響を与えずに出入りできる状態をどのように処理しますか?
たとえば、現在のリーフの状態に関係なく常に使用できる[新しいメッセージ]ボタン。[新しいメッセージ]をクリックすると、URLに影響を与えることなく、現在のビューでモーダルな新しいメッセージが開きます。
現在、私は次のようなアプローチを使用しています。
ルート:
App.Router.map(function() {
this.route('inbox');
this.route('archive');
});
App.IndexRoute = Em.Route.extend({
...
events: {
newMessage: function() {
this.render('new_message', { into: 'application', outlet: 'modalView' });
},
// Clicking 'Save' or 'Cancel' in the new message modal triggers this event to remove the view:
hideModal: function() {
// BAD - using private API
this.router._lookupActiveView('application').disconnectOutlet('modalView');
}
}
});
App.InboxRoute = Em.Route.extend({
...
renderTemplate: function(controller, model) {
// BAD - need to specify the application template, instead of using default implementation
this.render('inbox', { into: 'application' });
}
});
App.ArchiveRoute = ... // basically the same as InboxRoute
application.handlebars:
<button {{action newMessage}}>New Message</button>
{{outlet}}
{{outlet modalView}}
簡潔にするために、明らかにいくつかのコードを省略しました。
このアプローチは「機能します」が、上記の2つの問題があります。
- プライベートAPIを使用して、
hideModal
イベントハンドラーのモーダルビューを削除しています。 - すべてのサブルートでテンプレートを指定する必要があります。指定
application
しない場合renderTemplate
、モーダルを開いて閉じてから受信トレイ間を移動すると、のデフォルトの実装はアプリケーションではなくモーダルのテンプレートにレンダリングしようとします。およびアーカイブ状態(モーダルのテンプレートがlastRenderedTemplate
IndexRouteのになっているため)。
明らかに、これらの問題はどちらも取引を妨げるものではありませんが、私が見逃しているより良いアプローチがあるかどうか、またはこれが現在のルーターAPIの単なるギャップであるかどうかを知ることは素晴らしいことです。