I cannot figure out how to properly handle modal states / views with the new Ember router. More generally, how do you handle states that you can enter and exit without affecting the "main" state (URL)?
For example, the button "New message", which is always available regardless of the current state of the sheet. Clicking New Message should open a new text message for the current view without affecting the URL.
I am currently using this approach:
Routes
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}}
I obviously forgot about some code for brevity.
This approach "works", but has two problems mentioned above:
- I use the private API to remove the modal view in the
hideModal event hideModal . - I need to specify the
application template in all my routines, because if I do not, the default implementation of renderTemplate will try to display in the modal template instead of the application, if you open the modal, close it, and then go between the input and archive states (since modal template became lastRenderedTemplate for IndexRoute).
Obviously, none of these problems is a transaction handler, but it would be nice to find out if there is a better approach that I am missing, or is it just a gap in the current router API.
Nick ragaz
source share