Skip to content

Commit c6aa4d3

Browse files
committed
[fixed] Now execute willTransition* hooks even if only query part was changed
Previously query change didn't fire hooks. Now does.
1 parent 58d6650 commit c6aa4d3

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

modules/__tests__/Router-test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,46 @@ describe('Router', function () {
5353
done();
5454
});
5555
});
56+
57+
it('execute willTransition* callbacks when query changes', function (done) {
58+
var fromCallbackExecuted = false;
59+
var Spoon = React.createClass({
60+
statics: {
61+
willTransitionTo: function (transition, params, query) {
62+
if (query['filter'] === 'first') {
63+
return; // skip first transition
64+
}
65+
66+
expect(query['filter']).toEqual('second');
67+
expect(fromCallbackExecuted).toBe(true);
68+
done();
69+
},
70+
71+
willTransitionFrom: function (transition, element) {
72+
fromCallbackExecuted = true;
73+
}
74+
},
75+
76+
render: function () {
77+
return <h1>Spoon</h1>;
78+
}
79+
});
80+
81+
var routes = (
82+
<Route handler={Nested} path='/'>
83+
<Route name="spoon" handler={Spoon}/>
84+
</Route>
85+
);
86+
87+
TestLocation.history = [ '/spoon?filter=first' ];
88+
89+
var div = document.createElement('div');
90+
Router.run(routes, TestLocation, function (Handler, state) {
91+
React.render(<Handler/>, div);
92+
});
93+
94+
TestLocation.push('/spoon?filter=second');
95+
});
5696
});
5797

5898
describe('willTransitionFrom', function () {
@@ -87,7 +127,6 @@ describe('Router', function () {
87127
});
88128
});
89129
});
90-
91130
});
92131

93132
});

modules/utils/createRouter.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,16 @@ function createRouter(options) {
298298
toRoutes = nextRoutes;
299299
}
300300

301+
// If routes' hooks arrays are empty, then we transition to current route.
302+
// But path is somehow still get changed.
303+
// That could be only because of route query changes.
304+
// Need to push current route to routes' hooks arrays.
305+
if (!toRoutes.length && !fromRoutes.length) {
306+
var currentRoute = state.routes[state.routes.length-1];
307+
fromRoutes = [currentRoute];
308+
toRoutes = [currentRoute];
309+
}
310+
301311
var transition = new Transition(path, this.replaceWith.bind(this, path));
302312

303313
transition.from(fromRoutes, components, function (error) {

0 commit comments

Comments
 (0)