KoalaKid
KoalaKid

Reputation: 226

Backbone Route not firing

This should be super basic but I can't get routing working. I should mention that the application is located in a subdirectory called /dist/. Here's my code:

    var QuestionRouter = Backbone.Router.extend({

    routes: {
      "/dist/" : "startTest"
      "dist/:id": "getModel"
    },

    startTest: function(){
      console.log('home called')
    },

    getModel: function(){
        app.getModel(id);
    }

});


  var app = new QuestionView;
  var appRouter = new QuestionRouter;
  Backbone.history.start({pushState: true});

The URL to trigger this route is:

    www.example.com/dist/
    www.example.com/dist/12345

Any help would be appreciated.

Upvotes: 0

Views: 400

Answers (2)

KoalaKid
KoalaKid

Reputation: 226

Ok So I was able to work this out:

  1. My route hash should look like this:

    routes: { "" : "startTest", ":id": "getModel" }

  2. I had to remove pushState: true, with this in place the route wasn't being triggered, not sure why:

    Backbone.history.start();

Upvotes: 0

gerardor
gerardor

Reputation: 39

You'll need to use # (hash symbol).

Backbone routers are used for routing your applications URL's when using hash tags(#)

This is a quote from a Backbone tutorial: What is a router?

See Backbone's Router documentation

Then your routes would be:

www.example.com/#/dist/
www.example.com/#/dist/12345

You can also use Backbone routes without hashes.

Upvotes: 3

Related Questions