Skip to content

Commit a38ff2a

Browse files
committed
Fix route depth
1 parent 7278dc5 commit a38ff2a

File tree

5 files changed

+51
-85
lines changed

5 files changed

+51
-85
lines changed

modules/__tests__/Router-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ describe('Router', function () {
5656
});
5757

5858
describe('willTransitionFrom', function () {
59-
it('sends a rendered component', function (done) {
59+
it('sends a rendered element', function (done) {
6060
var div = document.createElement('div');
6161

6262
var Bar = React.createClass({
6363
statics: {
64-
willTransitionFrom: function (transition, component) {
65-
expect(div.querySelector('#bar')).toEqual(component.getDOMNode());
64+
willTransitionFrom: function (transition, element) {
65+
expect(div.querySelector('#bar')).toEqual(element.getDOMNode());
6666
done();
6767
}
6868
},

modules/elements/RouteHandler.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,41 @@ var RouteHandler = React.createClass({
1515
},
1616

1717
contextTypes: {
18-
pushRouteHandlerElement: React.PropTypes.func.isRequired,
19-
popRouteHandlerElement: React.PropTypes.func.isRequired,
20-
getCurrentRouteAtDepth: React.PropTypes.func.isRequired
18+
getElements: React.PropTypes.func.isRequired,
19+
getRouteAtDepth: React.PropTypes.func.isRequired,
20+
routeHandlers: React.PropTypes.array.isRequired
2121
},
2222

23-
componentWillMount: function () {
24-
this._routeDepth = this.context.pushRouteHandlerElement(this);
23+
childContextTypes: {
24+
routeHandlers: React.PropTypes.array.isRequired
2525
},
2626

27-
componentWillUnmount: function () {
28-
this.context.popRouteHandlerElement(this);
27+
getChildContext: function () {
28+
return {
29+
routeHandlers: this.context.routeHandlers.concat([ this ])
30+
};
31+
},
32+
33+
getRouteDepth: function () {
34+
return this.context.routeHandlers.length - 1;
35+
},
36+
37+
componentDidMount: function () {
38+
this._updateElement();
39+
},
40+
41+
componentDidUpdate: function () {
42+
this._updateElement();
2943
},
3044

31-
/**
32-
* Returns the route handler's element.
33-
*/
34-
getHandlerElement: function () {
35-
return this.refs[this.props.ref];
45+
_updateElement: function () {
46+
var depth = this.getRouteDepth();
47+
var elements = this.context.getElements();
48+
elements[depth] = this.refs[this.props.ref];
3649
},
3750

3851
render: function () {
39-
var route = this.context.getCurrentRouteAtDepth(this._routeDepth);
52+
var route = this.context.getRouteAtDepth(this.getRouteDepth());
4053
return route ? React.createElement(route.handler, this.props) : null;
4154
}
4255

modules/mixins/RouteHandlerContext.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

modules/utils/Transition.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ function runTransitionFromHooks(transition, routes, elements, callback) {
4444
var hooks = reversedArray(routes).map(function (route, index) {
4545
return function () {
4646
var handler = route.handler;
47-
var element = elements[index];
4847

4948
if (!transition.isAborted && handler.willTransitionFrom)
50-
return handler.willTransitionFrom(transition, element && element.getHandlerElement());
49+
return handler.willTransitionFrom(transition, elements[index]);
5150

5251
var promise = transition._promise;
5352
transition._promise = null;

modules/utils/createRouter.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var HashLocation = require('../locations/HashLocation');
88
var HistoryLocation = require('../locations/HistoryLocation');
99
var NavigationContext = require('../mixins/NavigationContext');
1010
var StateContext = require('../mixins/StateContext');
11-
var RouteHandlerContext = require('../mixins/RouteHandlerContext');
1211
var Scrolling = require('../mixins/Scrolling');
1312
var createRoutesFromChildren = require('./createRoutesFromChildren');
1413
var supportsHistory = require('./supportsHistory');
@@ -149,7 +148,7 @@ function createRouter(options) {
149148

150149
displayName: 'Router',
151150

152-
mixins: [ NavigationContext, StateContext, RouteHandlerContext, Scrolling ],
151+
mixins: [ NavigationContext, StateContext, Scrolling ],
153152

154153
statics: {
155154

@@ -379,6 +378,11 @@ function createRouter(options) {
379378
return elements;
380379
},
381380

381+
getRouteAtDepth: function (depth) {
382+
var routes = this.state.routes;
383+
return routes && routes[depth];
384+
},
385+
382386
getInitialState: function () {
383387
return state;
384388
},
@@ -388,8 +392,21 @@ function createRouter(options) {
388392
},
389393

390394
render: function () {
391-
var routes = this.state.routes;
392-
return routes && routes.length ? React.createElement(RouteHandler, this.props) : null;
395+
return this.getRouteAtDepth(0) ? React.createElement(RouteHandler, this.props) : null;
396+
},
397+
398+
childContextTypes: {
399+
getElements: React.PropTypes.func.isRequired,
400+
getRouteAtDepth: React.PropTypes.func.isRequired,
401+
routeHandlers: React.PropTypes.array.isRequired
402+
},
403+
404+
getChildContext: function () {
405+
return {
406+
getElements: this.getElements,
407+
getRouteAtDepth: this.getRouteAtDepth,
408+
routeHandlers: [ this ]
409+
};
393410
}
394411

395412
});

0 commit comments

Comments
 (0)