Permalink
Browse files

Route helpers account for root.

  • Loading branch information...
1 parent 0505b06 commit d47aead8286b8f65b61eea98f0b2218439bb69b5 Dan Gilbert committed Feb 20, 2012
Showing with 36 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +2 −0 backbone_named_routes.js
  3. +33 −1 spec/backbone_named_routes_spec.js
View
@@ -0,0 +1 @@
+.DS_Store
@@ -7,6 +7,8 @@ Backbone.NamedRoutes = {
pathFor: function(pathPattern, urlParams, queryParams) {
var path = pathPattern;
if (path[0] !== "/") path = "/" + path;
+ if (Backbone.history.options && Backbone.history.options.root !== "/") path = Backbone.history.options.root + path;
+
for(var i = 0; i < urlParams.length; i++) {
var param = urlParams[i];
path = path.replace(/\:\w+/, param);
@@ -8,9 +8,18 @@ describe("Backbone named routes extension", function() {
}
});
+ var router = new Workspace();
+
+ beforeEach(function() {
+ Backbone.history.options = null;
+
+ spyOn(Backbone.history, 'start').andCallFake(function(options) {
+ Backbone.history.options = options;
+ });
+ });
+
describe("Views", function() {
it("provides a helper with named routes", function() {
- var router = new Workspace();
var view = new Backbone.View();
expect(view.helper.helpPath()).toEqual("/help");
@@ -26,6 +35,17 @@ describe("Backbone named routes extension", function() {
expect(view.helper.searchPath('kiwis', { foo: "bar", baz: "boo" })).toEqual("/search/kiwis?foo=bar&baz=boo");
expect(view.helper.searchPath('kiwis', 7, { foo: "bar", baz: "boo" })).toEqual("/search/kiwis/p7?foo=bar&baz=boo");
});
+
+ it("includes the root if history has been started", function() {
+ var router = new Workspace();
+ var view = new Backbone.View();
+
+ Backbone.history.start({ pushState: true, root: "/articles" });
+
+ expect(view.helper.helpPath({ foo: "bar", baz: "boo" })).toEqual("/articles/help?foo=bar&baz=boo");
+ expect(view.helper.searchPath('kiwis', { foo: "bar", baz: "boo" })).toEqual("/articles/search/kiwis?foo=bar&baz=boo");
+ expect(view.helper.searchPath('kiwis', 7, { foo: "bar", baz: "boo" })).toEqual("/articles/search/kiwis/p7?foo=bar&baz=boo");
+ });
});
describe("Routers", function() {
@@ -45,6 +65,16 @@ describe("Backbone named routes extension", function() {
expect(router.helper.searchPath('kiwis', { foo: "bar", baz: "boo" })).toEqual("/search/kiwis?foo=bar&baz=boo");
expect(router.helper.searchPath('kiwis', 7, { foo: "bar", baz: "boo" })).toEqual("/search/kiwis/p7?foo=bar&baz=boo");
});
+
+ it("includes the root if history has been started", function() {
+ var router = new Workspace();
+
+ Backbone.history.start({ pushState: true, root: "/articles" });
+
+ expect(router.helper.helpPath({ foo: "bar", baz: "boo" })).toEqual("/articles/help?foo=bar&baz=boo");
+ expect(router.helper.searchPath('kiwis', { foo: "bar", baz: "boo" })).toEqual("/articles/search/kiwis?foo=bar&baz=boo");
+ expect(router.helper.searchPath('kiwis', 7, { foo: "bar", baz: "boo" })).toEqual("/articles/search/kiwis/p7?foo=bar&baz=boo");
+ });
});
it("allows implementors to mix in the url helper into other places", function() {
@@ -59,4 +89,6 @@ describe("Backbone named routes extension", function() {
expect(model.helper.searchPath('kiwis')).toEqual("/search/kiwis");
expect(model.helper.searchPath('kiwis', 7)).toEqual("/search/kiwis/p7");
});
+
+
});

0 comments on commit d47aead

Please sign in to comment.