My app uses modals pretty heavily and I’ve been converting from routeless modals with controllers to routed modals where it makes sense. One thing I’m running into is when I close the modal the URL needs to update to the proper URL.
// List of all cars for a manufacturer
/manufacturers/1/cars
// View details of a specific car
/manufacturers/1/cars/3
If /manufacturers/1/cars/3
is open in a modal, closing that modal should transition me back to /manufacturers/1/cars
I’m currently handling this by saving the transition in /manufacturers/:id
and then retrying that transition on modal close.
App.ManufacturersManufacturerRoute = Ember.Route.extend({
beforeModel(transition, queryParams) {
this.controllerFor('application').set('previousTransition', transition);
}
});
App.ManufacturersManufacturerCarsViewRoute = Ember.Route.extend({
defaultTransitionRoute: 'manufacturers.manufacturer',
willTransition: function(transition) {
var self = this;
// transition to the previous route
// if the previous route is the same as the current route then redirect to a leaf route further up the tree
var previousTransition = self.controllerFor('application').get('previousTransition');
if (previousTransition.targetName === self.routeName) {
// reset the transition to the immediately previous transition that has a param value
var route = '', params = '';
$.each(previousTransition.handlerInfos.reverse(), function(i, handler) {
if (handler.name !== self.routeName) {
var keys = Ember.keys(handler.params);
if (keys.length > 0) {
route = handler.name;
params = handler.params[keys[0]];
return false;
}
}
});
if (!Ember.isEmpty(params)) {
self.transitionTo(route, params);
} else {
self.transitionTo(self.get('defaultTransitionRoute'));
}
} else {
previousTransition.retry();
},
actions: {
closeModal: function () {
this.disconnectOutlet({
outlet: 'modal',
parentView: 'application'
});
var previousTransition = this.controllerFor('application').get('previousTransition');
return previousTransition.retry();
}
}
});
In the case where manufacturers\1\cars\3
is directly opened the previousTransition
is the same as the current route. In this case I want to transition to the leaf-most route with a valid param (always the parent object route in my case). previousTransition.retry()
in closeModal
is being hit but the willTransition
event is not firing as I would expect it to.
Is it better to have the willTransition
logic in my closeModal
method or is there a way to force willTransition
to fire?