Skip to content

Commit 4d8c7a1

Browse files
committed
Fixed everything
[removed] <Routes onTransitionError> [removed] <Routes onAbortedTransition>
1 parent c840bea commit 4d8c7a1

File tree

7 files changed

+36
-182
lines changed

7 files changed

+36
-182
lines changed

modules/actions/LocationActions.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ var LocationActions = {
2121
/**
2222
* Indicates the most recent entry should be removed from the history stack.
2323
*/
24-
POP: 'pop',
25-
26-
/**
27-
* Indicates that a route transition is finished.
28-
*/
29-
FINISHED_TRANSITION: 'finished-transition'
24+
POP: 'pop'
3025

3126
};
3227

modules/components/Routes.js

+7-38
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ var React = require('react');
22
var warning = require('react/lib/warning');
33
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
44
var copyProperties = require('react/lib/copyProperties');
5-
var LocationActions = require('../actions/LocationActions');
6-
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
75
var PathStore = require('../stores/PathStore');
8-
var ScrollStore = require('../stores/ScrollStore');
96
var reversedArray = require('../utils/reversedArray');
107
var Transition = require('../utils/Transition');
118
var Redirect = require('../utils/Redirect');
@@ -265,13 +262,6 @@ function computeHandlerProps(matches, query) {
265262

266263
var BrowserTransitionHandling = {
267264

268-
handleTransition: function (transition) {
269-
LocationDispatcher.handleViewAction({
270-
type: LocationActions.FINISHED_TRANSITION,
271-
path: transition.path
272-
});
273-
},
274-
275265
handleTransitionError: function (error) {
276266
throw error; // This error probably originated in a transition hook.
277267
},
@@ -290,10 +280,6 @@ var BrowserTransitionHandling = {
290280

291281
var ServerTransitionHandling = {
292282

293-
handleTransition: function (transition) {
294-
// TODO
295-
},
296-
297283
handleTransitionError: function (error) {
298284
// TODO
299285
},
@@ -330,20 +316,9 @@ var Routes = React.createClass({
330316
mixins: [ ActiveContext, LocationContext, RouteContext, ScrollContext ],
331317

332318
propTypes: {
333-
onTransition: React.PropTypes.func.isRequired,
334-
onTransitionError: React.PropTypes.func.isRequired,
335-
onAbortedTransition: React.PropTypes.func.isRequired,
336319
initialPath: React.PropTypes.string
337320
},
338321

339-
getDefaultProps: function () {
340-
return {
341-
onTransition: TransitionHandling.handleTransition,
342-
onTransitionError: TransitionHandling.handleTransitionError,
343-
onAbortedTransition: TransitionHandling.handleAbortedTransition
344-
};
345-
},
346-
347322
getInitialState: function () {
348323
return {
349324
matches: []
@@ -356,41 +331,35 @@ var Routes = React.createClass({
356331

357332
componentDidMount: function () {
358333
PathStore.addChangeListener(this.handlePathChange);
359-
ScrollStore.addChangeListener(this.handleScrollChange);
360334
},
361335

362336
componentWillUnmount: function () {
363-
ScrollStore.removeChangeListener(this.handleScrollChange);
364337
PathStore.removeChangeListener(this.handlePathChange);
365338
},
366339

367340
handlePathChange: function (_path) {
368341
var path = _path || PathStore.getCurrentPath();
342+
var actionType = PathStore.getCurrentActionType();
369343

370344
if (this.state.path === path)
371345
return; // Nothing to do!
372346

347+
if (this.state.path)
348+
this.recordScroll(this.state.path);
349+
373350
var self = this;
374351

375352
this.dispatch(path, function (error, transition) {
376353
if (error) {
377-
self.props.onTransitionError.call(self, error);
354+
TransitionHandling.handleTransitionError.call(self, error);
378355
} else if (transition.isAborted) {
379-
self.props.onAbortedTransition.call(self, transition);
356+
TransitionHandling.handleAbortedTransition.call(self, transition);
380357
} else {
381-
self.props.onTransition.call(self, transition);
358+
self.updateScroll(path, actionType);
382359
}
383360
});
384361
},
385362

386-
handleScrollChange: function () {
387-
var behavior = this.getScrollBehavior();
388-
var position = ScrollStore.getCurrentScrollPosition();
389-
390-
if (behavior && position)
391-
behavior.updateScrollPosition(position, PathStore.getCurrentActionType());
392-
},
393-
394363
/**
395364
* Performs a depth-first search for the first route in the tree that matches on
396365
* the given path. Returns an array of all routes in the tree leading to the one

modules/components/__tests__/Routes-test.js

-54
Original file line numberDiff line numberDiff line change
@@ -69,58 +69,4 @@ describe('A Routes', function () {
6969
});
7070
});
7171

72-
73-
describe('when a transition is aborted', function () {
74-
it('triggers onAbortedTransition', function (done) {
75-
var AbortHandler = React.createClass({
76-
statics: {
77-
willTransitionTo: function (transition) {
78-
transition.abort();
79-
}
80-
},
81-
render: function () {
82-
return null;
83-
}
84-
});
85-
86-
function handleAbortedTransition(transition) {
87-
assert(transition);
88-
done();
89-
}
90-
91-
ReactTestUtils.renderIntoDocument(
92-
Routes({ onAbortedTransition: handleAbortedTransition },
93-
Route({ handler: AbortHandler })
94-
)
95-
);
96-
});
97-
});
98-
99-
describe('when there is an error in a transition hook', function () {
100-
it('triggers onTransitionError', function (done) {
101-
var ErrorHandler = React.createClass({
102-
statics: {
103-
willTransitionTo: function (transition) {
104-
throw new Error('boom!');
105-
}
106-
},
107-
render: function () {
108-
return null;
109-
}
110-
});
111-
112-
function handleTransitionError(error) {
113-
assert(error);
114-
expect(error.message).toEqual('boom!');
115-
done();
116-
}
117-
118-
ReactTestUtils.renderIntoDocument(
119-
Routes({ onTransitionError: handleTransitionError },
120-
Route({ handler: ErrorHandler })
121-
)
122-
);
123-
});
124-
});
125-
12672
});

modules/mixins/ScrollContext.js

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
44
var ImitateBrowserBehavior = require('../behaviors/ImitateBrowserBehavior');
55
var ScrollToTopBehavior = require('../behaviors/ScrollToTopBehavior');
66

7+
function getWindowScrollPosition() {
8+
invariant(
9+
canUseDOM,
10+
'Cannot get current scroll position without a DOM'
11+
);
12+
13+
return {
14+
x: window.scrollX,
15+
y: window.scrollY
16+
};
17+
}
18+
719
/**
820
* A hash of { name: scrollBehavior } pairs.
921
*/
@@ -52,6 +64,22 @@ var ScrollContext = {
5264
behavior == null || canUseDOM,
5365
'Cannot use scroll behavior without a DOM'
5466
);
67+
68+
if (behavior != null)
69+
this._scrollPositions = {};
70+
},
71+
72+
recordScroll: function (path) {
73+
if (this._scrollPositions)
74+
this._scrollPositions[path] = getWindowScrollPosition();
75+
},
76+
77+
updateScroll: function (path, actionType) {
78+
var behavior = this.getScrollBehavior();
79+
var position = this._scrollPositions[path];
80+
81+
if (behavior && position)
82+
behavior.updateScrollPosition(position, actionType);
5583
},
5684

5785
/**

modules/stores/PathStore.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var EventEmitter = require('events').EventEmitter;
22
var LocationActions = require('../actions/LocationActions');
33
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
4-
var ScrollStore = require('./ScrollStore');
54

65
var CHANGE_EVENT = 'change';
76
var _events = new EventEmitter;
@@ -51,8 +50,6 @@ var PathStore = {
5150
case LocationActions.PUSH:
5251
case LocationActions.REPLACE:
5352
case LocationActions.POP:
54-
LocationDispatcher.waitFor([ ScrollStore.dispatchToken ]);
55-
5653
if (_currentPath !== action.path) {
5754
_currentPath = action.path;
5855
_currentActionType = action.type;

modules/stores/ScrollStore.js

-79
This file was deleted.

tests.js

-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ require('./modules/utils/__tests__/Path-test');
1616

1717

1818
var PathStore = require('./modules/stores/PathStore');
19-
var ScrollStore = require('./modules/stores/ScrollStore');
2019

2120
afterEach(function () {
2221
// For some reason unmountComponentAtNode doesn't call componentWillUnmount :/
2322
PathStore.removeAllChangeListeners();
24-
ScrollStore.removeAllChangeListeners();
2523
});

0 commit comments

Comments
 (0)