Skip to content

Commit

Permalink
add baseURL to allow namespaced routes
Browse files Browse the repository at this point in the history
  • Loading branch information
vieron committed Nov 3, 2015
1 parent e447ed3 commit 112f8ba
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion backbone.subroute.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
}(function(_, Backbone) {

Backbone.SubRoute = Backbone.Router.extend({

baseURL: '',

constructor: function(prefix, options) {
options = _.defaults(options || {}, {
createTrailingSlashRoutes: false,
Expand All @@ -37,6 +40,8 @@
// between the prefix and the sub-route path for each route that we register with Backbone.
this.separator = (prefix.slice(-1) === "/") ? "" : "/";

this.baseURL = (this.baseURL.slice(-1) === "/") ? this.baseURL : this.baseURL + "/";

// if you want to match "books" and "books/" without creating separate routes, set this
// option to "true" and the sub-router will automatically create those routes for you.
this.createTrailingSlashRoutes = options.createTrailingSlashRoutes;
Expand Down Expand Up @@ -74,7 +79,7 @@
if (route.substr(0, 1) != '/' &&
route.indexOf(this.prefix.substr(0, this.prefix.length - 1)) !== 0) {

route = this.prefix +
route = this.baseURL + this.prefix +
(route ? this.separator : "") +
route;
}
Expand All @@ -95,6 +100,8 @@
_route += route;
}

_route = this.baseURL + _route;

if (this.createTrailingSlashRoutes) {
this.routes[_route + '/'] = name;
Backbone.Router.prototype.route.call(this, _route + '/', name, callback);
Expand Down

0 comments on commit 112f8ba

Please sign in to comment.