Skip to content

Commit

Permalink
[fixed] Now execute willTransition* hooks even if only query part was…
Browse files Browse the repository at this point in the history
… changed

Previously query change didn't fire hooks. Now does.
  • Loading branch information
vladimir-vg committed Nov 23, 2014
1 parent 58d6650 commit c6aa4d3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
41 changes: 40 additions & 1 deletion modules/__tests__/Router-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <h1>Spoon</h1>;
}
});

var routes = (
<Route handler={Nested} path='/'>
<Route name="spoon" handler={Spoon}/>
</Route>
);

TestLocation.history = [ '/spoon?filter=first' ];

var div = document.createElement('div');
Router.run(routes, TestLocation, function (Handler, state) {
React.render(<Handler/>, div);
});

TestLocation.push('/spoon?filter=second');
});
});

describe('willTransitionFrom', function () {
Expand Down Expand Up @@ -87,7 +127,6 @@ describe('Router', function () {
});
});
});

});

});
Expand Down
10 changes: 10 additions & 0 deletions modules/utils/createRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c6aa4d3

Please sign in to comment.