1

I have got three routes like this:

var appRouter = Backbone.Router.extend({
    routes: {
        "": "index",
        "questionnaire/info/id/:id": "questionnaireInfo",
        "questions/edit/*params": "questionEdit"
    },
    questionnaireInfo: function(id) {
        $('#app-body').load('/dashboard/questionnaire/info/id/' + id);
    },
    questionEdit: function(questionnaireId) {
        console.log(questionnaireId, params);
    },
    index: function() {
        console.log('index');
    }
});

And i initialize them like this:

var appRouting = new appRouter;
Backbone.history.start({
    pushState: true,
    silent: false,
    root: '/dashboard/'
});

On first page load the route matches, it even console.log the proper messages. But i have got a link element like this:

<a href="/dashboard">Home Page</a>

It doesn't match the "" route. And this href element doesn't match the "questionnaire/info/id/:id" route:

<a href="/dashboard/questionnaire/info/id/1">Load</a>

How can i make this working? Thanks.

cnkt
  • 2,943
  • 5
  • 30
  • 43

4 Answers4

0

Maybe you're missing the '/' between '/dashboard' and each route

alexandernst
  • 14,352
  • 22
  • 97
  • 197
  • I don't understand your answer. I already defined it as root. Why should i prepend it too all routes. – cnkt Jul 20 '12 at 11:15
  • I mean, your rule is "questions/edit/*params", but the actual path is "/questions/edit/*params" (note the '/' at the beggining). – alexandernst Jul 20 '12 at 11:21
  • the route with / prepended doesn't match either. – cnkt Jul 20 '12 at 11:23
  • According to docs: If your application is not being served from the root url / of your domain, be sure to tell History where the root really is, as an option: Backbone.history.start({pushState: true, root: "/public/search/"}). So, try appending '/' to the root and leave your rules as they were. If that doesn't help, try routing with regex ( this.route(/regex/, function); ) – alexandernst Jul 20 '12 at 11:28
  • i appended it: root: '/dashboard/' still doesnt work, and i also tried regex. – cnkt Jul 20 '12 at 11:30
  • can you paste here one of the regex rules? – alexandernst Jul 20 '12 at 11:34
  • sure, i write a regex only for testing purposes /^edit\/([a-z] )$/ it should match edit/test. – cnkt Jul 20 '12 at 11:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14176/discussion-between-alexandernst-and-cnkt) – alexandernst Jul 20 '12 at 11:44
0

is index firing? in general the backbone docs recommend listing your routes from most specific to least specific. It's possible that the other roots aren't being called because "":"index" catches everything.

dcole2929
  • 357
  • 2
  • 11
0

You need a trailing slash in your Backbone.history.start call for the root argument.

var appRouting = new appRouter;
Backbone.history.start({
    pushState: true,
    silent: false,
    root: '/dashboard/'
});

You can see this in the example for Backbone.history in the docs.

tkone
  • 22,092
  • 5
  • 54
  • 78
0

I had the same problem. It seems that the links that call the routes have to be prepended by a "#" character. So if the root of the router is

/dashboard/

and your route is

"questionnaire/info/id/:id": "questionnaireInfo"

than the link triggering the route should be for example

<a href="/dashboard/#/questionnaire/info/id/1">Load</a>

Hope this helps.

alexandru
  • 103
  • 5