Skip to content

Commit 7dd8ac1

Browse files
committed
Contextual routers: fix ownership and environment inference
- Contextual routers are only can be in context of other routers if they share the same environment. - If contextual routers isn't given an environment (via `hash` or `environment` prop) then it is default to an environment from parent's router if any.
1 parent 210dda2 commit 7dd8ac1

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

lib/Environment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ DummyEnvironment.prototype.stop = emptyFunction;
204204
var Mixin = {
205205

206206
componentDidMount: function() {
207-
this.props.environment.register(this);
207+
this.getEnvironment().register(this);
208208
},
209209

210210
componentWillUnmount: function() {
211-
this.props.environment.unregister(this);
211+
this.getEnvironment().unregister(this);
212212
}
213213
};
214214

lib/RouterMixin.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ var RouterMixin = {
1818

1919
getDefaultProps: function() {
2020
return {
21-
environment: this.props.hash ?
22-
Environment.hashEnvironment :
23-
Environment.defaultEnvironment,
2421
onBeforeNavigation: emptyFunction,
2522
onNavigation: emptyFunction
2623
};
@@ -53,9 +50,11 @@ var RouterMixin = {
5350
var path;
5451
var prefix;
5552

56-
if (props.contextual && this.context.router) {
53+
var parent = this.getParentRouter();
5754

58-
var match = this.context.router.getMatch();
55+
if (props.contextual && parent) {
56+
57+
var match = parent.getMatch();
5958

6059
invariant(
6160
props.path || isString(match.unmatchedPath),
@@ -66,7 +65,7 @@ var RouterMixin = {
6665
prefix = match.matchedPath;
6766
} else {
6867

69-
path = props.path || props.environment.getPath();
68+
path = props.path || this.getEnvironment().getPath();
7069

7170
invariant(
7271
isString(path),
@@ -88,11 +87,31 @@ var RouterMixin = {
8887
};
8988
},
9089

90+
getEnvironment: function() {
91+
if (this.props.environment) {
92+
return this.props.environment;
93+
}
94+
if (this.props.hash) {
95+
return Environment.hashEnvironment;
96+
}
97+
if (this.props.contextual && this.context.router) {
98+
return this.context.router.getEnvironment();
99+
}
100+
return Environment.defaultEnvironment;
101+
},
102+
91103
/**
92104
* Return parent router or undefined.
93105
*/
94106
getParentRouter: function() {
95-
return this.context.router;
107+
var current = this.context.router;
108+
var environment = this.getEnvironment();
109+
110+
while (current) {
111+
if (current.getEnvironment() === environment) {
112+
return current;
113+
}
114+
}
96115
},
97116

98117
/**
@@ -122,7 +141,7 @@ var RouterMixin = {
122141
}
123142
navigation = navigation || {};
124143
path = join(this.state.prefix, path);
125-
this.props.environment.setPath(path, navigation, cb);
144+
this.getEnvironment().setPath(path, navigation, cb);
126145
},
127146

128147
/**

0 commit comments

Comments
 (0)