From 193222ec5040e7e50603c902ce5dacf731869854 Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Fri, 13 Feb 2015 14:45:56 -0800 Subject: [PATCH] [added] StaticLocation, for server-side rendering --- modules/createRouter.js | 30 ++++++++--------------------- modules/index.js | 1 + modules/locations/StaticLocation.js | 23 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 modules/locations/StaticLocation.js diff --git a/modules/createRouter.js b/modules/createRouter.js index 92778773d9..7034d3cbf7 100644 --- a/modules/createRouter.js +++ b/modules/createRouter.js @@ -8,6 +8,7 @@ var ImitateBrowserBehavior = require('./behaviors/ImitateBrowserBehavior'); var HashLocation = require('./locations/HashLocation'); var HistoryLocation = require('./locations/HistoryLocation'); var RefreshLocation = require('./locations/RefreshLocation'); +var StaticLocation = require('./locations/StaticLocation'); var NavigationContext = require('./NavigationContext'); var StateContext = require('./StateContext'); var Scrolling = require('./Scrolling'); @@ -139,6 +140,8 @@ function createRouter(options) { 'You should not use a static location in a DOM environment because ' + 'the router will not be kept in sync with the current URL' ); + + location = new StaticLocation(location); } else { invariant( canUseDOM || location.needsDOM === false, @@ -240,11 +243,6 @@ function createRouter(options) { * a new URL onto the history stack. */ transitionTo: function (to, params, query) { - invariant( - typeof location !== 'string', - 'You cannot use transitionTo with a static location' - ); - var path = this.makePath(to, params, query); if (pendingTransition) { @@ -260,11 +258,6 @@ function createRouter(options) { * the current URL in the history stack. */ replaceWith: function (to, params, query) { - invariant( - typeof location !== 'string', - 'You cannot use replaceWith with a static location' - ); - location.replace(this.makePath(to, params, query)); }, @@ -280,11 +273,6 @@ function createRouter(options) { * because we cannot reliably track history length. */ goBack: function () { - invariant( - typeof location !== 'string', - 'You cannot use goBack with a static location' - ); - if (History.length > 1 || location === RefreshLocation) { location.pop(); return true; @@ -296,7 +284,7 @@ function createRouter(options) { }, handleAbort: options.onAbort || function (abortReason) { - if (typeof location === 'string') + if (location instanceof StaticLocation) throw new Error('Unhandled aborted transition! Reason: ' + abortReason); if (abortReason instanceof Cancellation) { @@ -431,17 +419,15 @@ function createRouter(options) { } }; - if (typeof location === 'string') { - Router.dispatch(location, null); - } else { + if (!(location instanceof StaticLocation)) { if (location.addChangeListener) location.addChangeListener(Router.handleLocationChange); this.isRunning = true; - - // Bootstrap using the current path. - this.refresh(); } + + // Bootstrap using the current path. + this.refresh(); }, refresh: function () { diff --git a/modules/index.js b/modules/index.js index f4acb203a3..145be66a99 100644 --- a/modules/index.js +++ b/modules/index.js @@ -8,6 +8,7 @@ exports.RouteHandler = require('./components/RouteHandler'); exports.HashLocation = require('./locations/HashLocation'); exports.HistoryLocation = require('./locations/HistoryLocation'); exports.RefreshLocation = require('./locations/RefreshLocation'); +exports.StaticLocation = require('./locations/StaticLocation'); exports.ImitateBrowserBehavior = require('./behaviors/ImitateBrowserBehavior'); exports.ScrollToTopBehavior = require('./behaviors/ScrollToTopBehavior'); diff --git a/modules/locations/StaticLocation.js b/modules/locations/StaticLocation.js new file mode 100644 index 0000000000..1f4f4e1c97 --- /dev/null +++ b/modules/locations/StaticLocation.js @@ -0,0 +1,23 @@ +var invariant = require('react/lib/invariant'); + +function throwCannotModify() { + invariant(false, 'You cannot modify a static location'); +} + +function StaticLocation(path) { + this.path = path; +} + +StaticLocation.prototype.push = throwCannotModify; +StaticLocation.prototype.replace = throwCannotModify; +StaticLocation.prototype.pop = throwCannotModify; + +StaticLocation.prototype.getCurrentPath = function () { + return this.path; +}; + +StaticLocation.prototype.toString = function () { + return ''; +}; + +module.exports = StaticLocation;