Skip to content

Commit

Permalink
[added] Ability to transitionTo absolute URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Sep 4, 2014
1 parent e5eddaf commit e0b708f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
29 changes: 21 additions & 8 deletions modules/actions/LocationActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
var LocationDispatcher = require('../dispatchers/LocationDispatcher');
var isAbsoluteURL = require('../utils/isAbsoluteURL');
var makePath = require('../utils/makePath');

function loadURL(url) {
window.location = url;
}

/**
* Actions that modify the URL.
*/
Expand All @@ -16,21 +21,29 @@ var LocationActions = {
* a new URL onto the history stack.
*/
transitionTo: function (to, params, query) {
LocationDispatcher.handleViewAction({
type: LocationActions.PUSH,
path: makePath(to, params, query)
});
if (isAbsoluteURL(to)) {
loadURL(to);
} else {
LocationDispatcher.handleViewAction({
type: LocationActions.PUSH,
path: makePath(to, params, query)
});
}
},

/**
* Transitions to the URL specified in the arguments by replacing
* the current URL in the history stack.
*/
replaceWith: function (to, params, query) {
LocationDispatcher.handleViewAction({
type: LocationActions.REPLACE,
path: makePath(to, params, query)
});
if (isAbsoluteURL(to)) {
loadURL(to);
} else {
LocationDispatcher.handleViewAction({
type: LocationActions.REPLACE,
path: makePath(to, params, query)
});
}
},

/**
Expand Down
11 changes: 11 additions & 0 deletions modules/utils/isAbsoluteURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var ABSOLUTE_URL_FORMAT = /^https?:\/\//;

/**
* Returns true if the given string contains an absolute URL
* according to http://tools.ietf.org/html/rfc3986#page-27.
*/
function isAbsoluteURL(string) {
return typeof string === 'string' && ABSOLUTE_URL_FORMAT.test(string);
}

module.exports = isAbsoluteURL;

0 comments on commit e0b708f

Please sign in to comment.