Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions modules/mixins/Scrolling.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,43 @@ function shouldUpdateScroll(state, prevState) {
*/
var Scrolling = {

statics: {
/**
* Records curent scroll position as the last known position for the given URL path.
*/
recordScrollPosition: function (path) {
if (!this.scrollHistory)
this.scrollHistory = {};

this.scrollHistory[path] = getWindowScrollPosition();
},

/**
* Returns the last known scroll position for the given URL path.
*/
getScrollPosition: function (path) {
if (!this.scrollHistory)
this.scrollHistory = {};

return this.scrollHistory[path] || null;
}
},

componentWillMount: function () {
invariant(
this.getScrollBehavior() == null || canUseDOM,
'Cannot use scroll behavior without a DOM'
);

this._scrollHistory = {};
},

componentDidMount: function () {
this._updateScroll();
},

componentWillUpdate: function () {
this._scrollHistory[this.state.path] = getWindowScrollPosition();
},

componentDidUpdate: function (prevProps, prevState) {
this._updateScroll(prevState);
},

componentWillUnmount: function () {
delete this._scrollHistory;
},

/**
* Returns the last known scroll position for the given URL path.
*/
getScrollPosition: function (path) {
return this._scrollHistory[path] || null;
},

_updateScroll: function (prevState) {
if (!shouldUpdateScroll(this.state, prevState)) {
return;
Expand All @@ -73,7 +78,7 @@ var Scrolling = {

if (scrollBehavior)
scrollBehavior.updateScrollPosition(
this.getScrollPosition(this.state.path),
this.constructor.getScrollPosition(this.state.path),
this.state.action
);
}
Expand Down
8 changes: 7 additions & 1 deletion modules/utils/createRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var invariant = require('react/lib/invariant');
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
var ImitateBrowserBehavior = require('../behaviors/ImitateBrowserBehavior');
var RouteHandler = require('../components/RouteHandler');
var LocationActions = require('../actions/LocationActions');
var HashLocation = require('../locations/HashLocation');
var HistoryLocation = require('../locations/HistoryLocation');
var RefreshLocation = require('../locations/RefreshLocation');
Expand Down Expand Up @@ -264,9 +265,14 @@ function createRouter(options) {
* hooks wait, the transition is fully synchronous.
*/
dispatch: function (path, action, callback) {
if (state.path === path)
var prevPath = state.path;
if (prevPath === path)
return; // Nothing to do!

if (prevPath && action !== LocationActions.REPLACE) {
this.recordScrollPosition(prevPath);
}

var match = this.match(path);

warning(
Expand Down