From c6aa4d3cc0a0af63c8f6ee48212834cc7040cd86 Mon Sep 17 00:00:00 2001 From: Vladimir Gordeev Date: Sun, 23 Nov 2014 03:33:24 +0200 Subject: [PATCH] [fixed] Now execute willTransition* hooks even if only query part was changed Previously query change didn't fire hooks. Now does. --- modules/__tests__/Router-test.js | 41 +++++++++++++++++++++++++++++++- modules/utils/createRouter.js | 10 ++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/modules/__tests__/Router-test.js b/modules/__tests__/Router-test.js index db8161aace..8d33ad8139 100644 --- a/modules/__tests__/Router-test.js +++ b/modules/__tests__/Router-test.js @@ -53,6 +53,46 @@ describe('Router', function () { done(); }); }); + + it('execute willTransition* callbacks when query changes', function (done) { + var fromCallbackExecuted = false; + var Spoon = React.createClass({ + statics: { + willTransitionTo: function (transition, params, query) { + if (query['filter'] === 'first') { + return; // skip first transition + } + + expect(query['filter']).toEqual('second'); + expect(fromCallbackExecuted).toBe(true); + done(); + }, + + willTransitionFrom: function (transition, element) { + fromCallbackExecuted = true; + } + }, + + render: function () { + return

Spoon

; + } + }); + + var routes = ( + + + + ); + + TestLocation.history = [ '/spoon?filter=first' ]; + + var div = document.createElement('div'); + Router.run(routes, TestLocation, function (Handler, state) { + React.render(, div); + }); + + TestLocation.push('/spoon?filter=second'); + }); }); describe('willTransitionFrom', function () { @@ -87,7 +127,6 @@ describe('Router', function () { }); }); }); - }); }); diff --git a/modules/utils/createRouter.js b/modules/utils/createRouter.js index 6aa91cb463..9f75af4a8c 100644 --- a/modules/utils/createRouter.js +++ b/modules/utils/createRouter.js @@ -298,6 +298,16 @@ function createRouter(options) { toRoutes = nextRoutes; } + // If routes' hooks arrays are empty, then we transition to current route. + // But path is somehow still get changed. + // That could be only because of route query changes. + // Need to push current route to routes' hooks arrays. + if (!toRoutes.length && !fromRoutes.length) { + var currentRoute = state.routes[state.routes.length-1]; + fromRoutes = [currentRoute]; + toRoutes = [currentRoute]; + } + var transition = new Transition(path, this.replaceWith.bind(this, path)); transition.from(fromRoutes, components, function (error) {