Skip to content

Commit

Permalink
Only try to prepend root on named routes that are auto-generated via …
Browse files Browse the repository at this point in the history
…the router.
  • Loading branch information
Dan Gilbert committed Sep 24, 2012
1 parent 83ec4e3 commit b655163
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
24 changes: 20 additions & 4 deletions backbone_named_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

var PATTERNS = {};

var addRoute = function(name, route) {
var addRoute = function(name, route, options) {
options = options || {};

_.defaults(options, {
includeRoot: false
});

// Create key to store path patterns for this route name.
PATTERNS[name] = PATTERNS[name] || {};

Expand All @@ -24,14 +30,24 @@
var routePattern = PATTERNS[name][numberOfParams];
var queryParams = hasQueryParams ? args.pop() : null;

if (options.includeRoot) routePattern = prependRoot(routePattern);

return pathFor(routePattern, args, queryParams);
};
};

var prependRoot = function(route) {
var history = Backbone.history;
if (!history) return route;
if (!history.options) return route;

routeWithRoot = history.options.root + '/' + route;
return routeWithRoot.replace("//", "/");
};

var pathFor = function(pathPattern, urlParams, queryParams) {
var path = pathPattern, history = Backbone.history;
if (path.charAt(0) !== "/") path = "/" + path;
if (history && history.options && history.options.root !== "/") path = history.options.root + path;

for(var i = 0; i < urlParams.length; i++) {
var param = urlParams[i];
Expand Down Expand Up @@ -71,7 +87,7 @@

Backbone.NamedRoutes = {

VERSION: '0.1.3',
VERSION: '0.1.4',

addRoute: addRoute
};
Expand All @@ -87,7 +103,7 @@
_(Backbone.Router.prototype).extend({
route: (function(original) {
return function(route, name, callback) {
addRoute(name, route);
addRoute(name, route, { includeRoot: true });
original.apply(this, arguments);
};
}(Backbone.Router.prototype.route))
Expand Down
31 changes: 30 additions & 1 deletion spec/backbone_named_routes_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe("Backbone named routes extension", function() {

var router = new Workspace();


Backbone.history.options = null;

spyOn(Backbone.history, 'start').andCallFake(function(options) {
Expand Down Expand Up @@ -125,5 +124,35 @@ describe("Backbone named routes extension", function() {
expect(model.helper.searchPath('kiwis')).toEqual("/search/kiwis");
expect(model.helper.searchPath('kiwis', 7)).toEqual("/search/kiwis/p7");
});

it("doesn't include the root on manually-created routes (by default)", function() {
var router = new Workspace();

Backbone.history.start({ pushState: true, root: "/articles" });

Backbone.NamedRoutes.addRoute('foo', '/foo/:id/bar/:id_2');
expect(router.helper.fooPath).toBeDefined();
expect(router.helper.fooPath('hello', 'world')).toEqual('/foo/hello/bar/world');
});

it("doesn't include the root on manually-created routes (when told not to)", function() {
var router = new Workspace();

Backbone.history.start({ pushState: true, root: "/articles" });

Backbone.NamedRoutes.addRoute('foo', '/foo/:id/bar/:id_2', { includeRoot: false });
expect(router.helper.fooPath).toBeDefined();
expect(router.helper.fooPath('hello', 'world')).toEqual('/foo/hello/bar/world');
});

it("includes the root on manually-created routes (when told to)", function() {
var router = new Workspace();

Backbone.history.start({ pushState: true, root: "/articles" });

Backbone.NamedRoutes.addRoute('foo', '/foo/:id/bar/:id_2', { includeRoot: true });
expect(router.helper.fooPath).toBeDefined();
expect(router.helper.fooPath('hello', 'world')).toEqual('/articles/foo/hello/bar/world');
});
});
});

0 comments on commit b655163

Please sign in to comment.