Skip to content
Browse files

Only try to prepend root on named routes that are auto-generated via …

…the router.
  • Loading branch information...
1 parent 83ec4e3 commit b655163c568c982641e679128e6a4a34c6354361 Dan Gilbert committed Sep 24, 2012
Showing with 50 additions and 5 deletions.
  1. +20 −4 backbone_named_routes.js
  2. +30 −1 spec/backbone_named_routes_spec.js
View
24 backbone_named_routes.js
@@ -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] || {};
@@ -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];
@@ -71,7 +87,7 @@
Backbone.NamedRoutes = {
- VERSION: '0.1.3',
+ VERSION: '0.1.4',
addRoute: addRoute
};
@@ -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))
View
31 spec/backbone_named_routes_spec.js
@@ -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) {
@@ -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.
Something went wrong with that request. Please try again.