documentcloud/backbone — Issue #996: Router calls the wrong method
Author: theindustry
Created: 2012-02-13 18:55:41+00:00
State: open

Perhaps I am just doing something wrong, but I think I've found a bug in the way `backbone.js` handles URI routing.

It appears that the behaviour of how Backbone handles URI's changes when I enter an URI into the browser compared to when I manually force the URI by invoking `myRouter.navigate()`.

Accessing all URI´s - `/contacts`, `/contacts/new` and `/contacts/ID` works well in the browser, but triggering `/contacts/new` manually fails as it instead calls the route method assigned to the `/contacts/ID` URI (loadContact).

To the code:

myApp.contactRouter = Backbone.Router.extend({

routes : {
"contacts": "contacts",
"contacts/new": "newContact",
"contacts/:id": "loadContact"
},

contacts: function() { console.log("in contacts") },

newContact: function() { console.log("Creating new contact"); },

loadContact: function(id) { console.log("Loading specific contact with id ", +id); }
});

And here's my failing `jasmine.js`-based unit test of the `newContact` method:

it("Opens /contacts/new" function() {

this.router.bind("route:newContact", this.routeSpy);
this.router.navigate("contacts/new", true);

this.router.bind("route:loadContact", function(route) { console.log(arguments) } ); // For debugging - loadContact is called instead!

expect(this.routeSpy.calledOnce).toBeTruthy(); // Fails
expect(this.routeSpy.calledWith("new")).toBeTruthy(); // Fails
});