Last active
August 29, 2015 14:07
-
-
Save davidworkman9/4f52c8029a7b3d3fbebb to your computer and use it in GitHub Desktop.
Drop in support for subpaths in iron-router.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
var url = Meteor.absoluteUrl(); | |
url = url.replace(/https?:\/\//, ''); | |
var slash = url.indexOf('/'); | |
var subpath = url.substring(slash); | |
subpath = subpath.substring(0, subpath.length-1); | |
var oldRoute = Router.route; | |
Router.route = function (name, options) { | |
arguments[1].path = subpath + options.path; | |
return oldRoute.apply(this, arguments); | |
}; | |
var oldGo = Router.go; | |
Router.go = function (route) { | |
var regex = new RegExp('^' + escapeRegex(subpath)); | |
if (route.indexOf('/') > -1 && !regex.test(route)) { | |
arguments[0] = subpath + route; | |
} | |
return oldGo.apply(this, arguments); | |
}; | |
function escapeRegex(str) { | |
return str.replace(/([/\\.?*()^${}|[\]])/g, '\\$1'); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment