Skip to content

Commit 17eac20

Browse files
author
Matt Marcum
committed
[BUGFIX beta] fixes transitionTo failing when called in beforeModel hook
1 parent 006dbf0 commit 17eac20

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

packages/ember-routing/lib/system/route.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -811,18 +811,27 @@ var Route = EmberObject.extend(ActionHandler, Evented, {
811811

812812
var handlerInfos = transition.state.handlerInfos;
813813
var router = this.router;
814-
var qpMeta = router._queryParamsFor(handlerInfos[handlerInfos.length - 1].name);
814+
var qpMeta;
815815
var changes = router._qpUpdates;
816816
var replaceUrl;
817817

818-
stashParamNames(router, handlerInfos);
818+
// handlerInfos are empty for some no-op/queryParamsOnly transitions
819+
if (!handlerInfos.length) {
820+
qpMeta = router._queryParamsFor(this.routeName);
821+
} else {
822+
qpMeta = router._queryParamsFor(handlerInfos[handlerInfos.length - 1].name);
823+
stashParamNames(router, handlerInfos);
824+
}
819825

820826
for (var i = 0; i < qpMeta.qps.length; ++i) {
821827
var qp = qpMeta.qps[i];
822828
var route = qp.route;
823829
var controller = route.controller;
824830
var presentKey = qp.urlKey in params && qp.urlKey;
825831

832+
if (!controller) {
833+
controller = route._addController();
834+
}
826835
// Do a reverse lookup to see if the changed query
827836
// param URL key corresponds to a QP property on
828837
// this controller.
@@ -1197,13 +1206,16 @@ var Route = EmberObject.extend(ActionHandler, Evented, {
11971206
}
11981207
},
11991208

1209+
12001210
/**
1201-
This hook is the entry point for router.js
1211+
Adds query param observers to controller and sets this.controller prop
12021212
1213+
@method _addController
1214+
@param {Object} context
12031215
@private
1204-
@method setup
12051216
*/
1206-
setup(context, transition) {
1217+
1218+
_addController(context) {
12071219
var controller;
12081220

12091221
var controllerName = this.controllerName || this.routeName;
@@ -1215,12 +1227,25 @@ var Route = EmberObject.extend(ActionHandler, Evented, {
12151227
controller = definedController;
12161228
}
12171229

1230+
var propNames = get(this, '_qp.propertyNames');
1231+
addQueryParamsObservers(controller, propNames);
1232+
this.controller = controller;
1233+
return controller;
1234+
},
1235+
1236+
/**
1237+
This hook is the entry point for router.js
1238+
1239+
@private
1240+
@method setup
1241+
*/
1242+
setup(context, transition) {
1243+
var controller = this.controller;
1244+
12181245
// Assign the route's controller so that it can more easily be
12191246
// referenced in action handlers. Side effects. Side effects everywhere.
1220-
if (!this.controller) {
1221-
var propNames = get(this, '_qp.propertyNames');
1222-
addQueryParamsObservers(controller, propNames);
1223-
this.controller = controller;
1247+
if (!controller) {
1248+
controller = this._addController(context);
12241249
}
12251250

12261251
var queryParams = get(this, '_qp');

packages/ember-routing/lib/system/router.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,9 @@ function calculatePostTransitionState(emberRouter, leafRouteName, contexts) {
987987

988988
function updatePaths(router) {
989989
let infos = router.router.currentHandlerInfos;
990+
991+
if (!Array.isArray(infos) || infos.length === 0) { return; }
992+
990993
let path = EmberRouter._routePath(infos);
991994
let currentRouteName = infos[infos.length - 1].name;
992995

packages/ember/tests/routing/query_params_test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,24 @@ if (isEnabled('ember-routing-route-configured-query-params')) {
622622
equal(router.get('location.path'), '/?foo=false', 'shorhand supported (bool)');
623623
});
624624

625+
QUnit.test('transitionTo called from route.beforeModel hook supports query params when configuration occurs on the route', function() {
626+
App.IndexRoute = Route.extend({
627+
queryParams: {
628+
foo: {
629+
defaultValue: 'lol'
630+
}
631+
},
632+
beforeModel(tranition) {
633+
this.transitionTo({queryParams: { foo: 'bar' }});
634+
}
635+
});
636+
637+
startingURL = "/?foo=lol";
638+
bootApplication();
639+
640+
equal(router.get('location.path'), '/?foo=bar');
641+
});
642+
625643
QUnit.test('transitionTo supports query params (multiple) when configuration occurs on the route', function() {
626644
App.IndexRoute = Route.extend({
627645
queryParams: {
@@ -2852,6 +2870,25 @@ if (isEnabled('ember-routing-route-configured-query-params')) {
28522870
equal(router.get('location.path'), '/?foo=false', 'shorhand supported (bool)');
28532871
});
28542872

2873+
QUnit.test('transitionTo called from route.beforeModel hook supports query params', function() {
2874+
App.IndexController = Controller.extend({
2875+
queryParams: ['foo'],
2876+
foo: 'lol'
2877+
});
2878+
2879+
App.IndexRoute = Route.extend({
2880+
beforeModel: function(transition) {
2881+
this.transitionTo({queryParams: { foo: 'bar' }});
2882+
}
2883+
});
2884+
startingURL = '/?foo=wat';
2885+
2886+
bootApplication();
2887+
2888+
equal(router.get('location.path'), '/?foo=bar');
2889+
});
2890+
2891+
28552892
QUnit.test('transitionTo supports query params (multiple)', function() {
28562893
App.IndexController = Controller.extend({
28572894
queryParams: ['foo', 'bar'],

0 commit comments

Comments
 (0)