Skip to content

Commit dbfebdd

Browse files
committed
Remove initialPath
This commit also changes the args that are passed to the dispatch callback. Instead of passing the Transition object, the signature is now callback(error, abortReason). Of course, both args are null/ undefined if there was neither an error or the transition was not aborted. This is going to give us a super-clean API for server-side rendering.
1 parent ce755d1 commit dbfebdd

File tree

2 files changed

+51
-57
lines changed

2 files changed

+51
-57
lines changed

modules/components/Routes.js

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var warning = require('react/lib/warning');
33
var invariant = require('react/lib/invariant');
44
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
55
var copyProperties = require('react/lib/copyProperties');
6-
var PathStore = require('../stores/PathStore');
76
var HashLocation = require('../locations/HashLocation');
87
var reversedArray = require('../utils/reversedArray');
98
var Transition = require('../utils/Transition');
@@ -264,13 +263,11 @@ function computeHandlerProps(matches, query) {
264263

265264
var BrowserTransitionHandling = {
266265

267-
handleTransitionError: function (component, error) {
266+
handleError: function (component, error) {
268267
throw error; // This error probably originated in a transition hook.
269268
},
270269

271-
handleAbortedTransition: function (component, transition) {
272-
var reason = transition.abortReason;
273-
270+
handleAbort: function (component, reason) {
274271
if (reason instanceof Redirect) {
275272
component.replaceWith(reason.to, reason.params, reason.query);
276273
} else {
@@ -282,11 +279,11 @@ var BrowserTransitionHandling = {
282279

283280
var ServerTransitionHandling = {
284281

285-
handleTransitionError: function (component, error) {
282+
handleError: function (component, error) {
286283
// TODO
287284
},
288285

289-
handleAbortedTransition: function (component, transition) {
286+
handleAbort: function (component, reason) {
290287
// TODO
291288
}
292289

@@ -312,7 +309,6 @@ var Routes = React.createClass({
312309
mixins: [ ActiveContext, LocationContext, RouteContext, ScrollContext ],
313310

314311
propTypes: {
315-
initialPath: React.PropTypes.string,
316312
onChange: React.PropTypes.func
317313
},
318314

@@ -322,44 +318,6 @@ var Routes = React.createClass({
322318
};
323319
},
324320

325-
componentWillMount: function () {
326-
this.handlePathChange(this.props.initialPath);
327-
},
328-
329-
componentDidMount: function () {
330-
PathStore.addChangeListener(this.handlePathChange);
331-
},
332-
333-
componentWillUnmount: function () {
334-
PathStore.removeChangeListener(this.handlePathChange);
335-
},
336-
337-
handlePathChange: function (_path) {
338-
var path = _path || PathStore.getCurrentPath();
339-
var actionType = PathStore.getCurrentActionType();
340-
341-
if (this.state.path === path)
342-
return; // Nothing to do!
343-
344-
if (this.state.path)
345-
this.recordScroll(this.state.path);
346-
347-
var self = this;
348-
349-
this.dispatch(path, function (error, transition) {
350-
if (error) {
351-
TransitionHandling.handleTransitionError(self, error);
352-
} else if (transition.isAborted) {
353-
TransitionHandling.handleAbortedTransition(self, transition);
354-
} else {
355-
self.updateScroll(path, actionType);
356-
357-
if (self.props.onChange)
358-
self.props.onChange.call(self);
359-
}
360-
});
361-
},
362-
363321
/**
364322
* Performs a depth-first search for the first route in the tree that matches on
365323
* the given path. Returns an array of all routes in the tree leading to the one
@@ -381,10 +339,32 @@ var Routes = React.createClass({
381339
return findMatches(Path.withoutQuery(path), this.getRoutes(), this.props.defaultRoute, this.props.notFoundRoute);
382340
},
383341

342+
updateLocation: function (path, actionType) {
343+
if (this.state.path === path)
344+
return; // Nothing to do!
345+
346+
if (this.state.path)
347+
this.recordScroll(this.state.path);
348+
349+
this.dispatch(path, actionType, function (error, abortReason) {
350+
if (error) {
351+
TransitionHandling.handleError(this, error);
352+
} else if (abortReason) {
353+
TransitionHandling.handleAbort(this, abortReason);
354+
} else {
355+
this.updateScroll(path, actionType);
356+
357+
if (this.props.onChange)
358+
this.props.onChange.call(this);
359+
}
360+
}.bind(this));
361+
},
362+
384363
/**
385-
* Performs a transition to the given path and calls callback(error, transition)
386-
* with the Transition object when the transition is finished and the component's
387-
* state has been updated accordingly.
364+
* Performs a transition to the given path and calls callback(error, abortReason)
365+
* when the transition is finished and the component's state has been updated. If
366+
* there was an error, the first argument will not be null. Otherwise, if the
367+
* transition was aborted for some reason, it will be given in the second arg.
388368
*
389369
* In a transition, the router first determines which routes are involved by
390370
* beginning with the current route, up the route tree to the first parent route
@@ -398,16 +378,13 @@ var Routes = React.createClass({
398378
*/
399379
dispatch: function (path, callback) {
400380
var transition = new Transition(this, path);
401-
var self = this;
402381

403382
computeNextState(this, transition, function (error, nextState) {
404-
if (error || nextState == null)
405-
return callback(error, transition);
383+
if (error || transition.isAborted || nextState == null)
384+
return callback(error, transition.abortReason);
406385

407-
self.setState(nextState, function () {
408-
callback(null, transition);
409-
});
410-
});
386+
this.setState(nextState, callback);
387+
}.bind(this));
411388
},
412389

413390
/**

modules/mixins/LocationContext.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,25 @@ var LocationContext = {
6161
'Cannot use location without a DOM'
6262
);
6363

64-
if (location)
64+
if (location) {
6565
PathStore.setup(location);
66+
this.handlePathChange();
67+
}
68+
},
69+
70+
componentDidMount: function () {
71+
if (this.getLocation())
72+
PathStore.addChangeListener(this.handlePathChange);
73+
},
74+
75+
componentWillUnmount: function () {
76+
if (this.getLocation())
77+
PathStore.removeChangeListener(this.handlePathChange);
78+
},
79+
80+
handlePathChange: function () {
81+
if (this.updateLocation)
82+
this.updateLocation(PathStore.getCurrentPath(), PathStore.getCurrentActionType());
6683
},
6784

6885
/**

0 commit comments

Comments
 (0)