TTT
TTT

Reputation: 6895

Backbone route to /

I have a Backbone Router:

class X.Routers.Main extends Backbone.Router

  routes:
    '/': 'home'
    'pageb': 'actionb'
    'pagec': 'actionc'

Pages B and C work, but navigating to http://domain.ext/ results in a page reload instead of triggering the right route.

How can I prevent this?

Upvotes: 11

Views: 3069

Answers (2)

Riki Pribadi
Riki Pribadi

Reputation: 94

  1. your base url path IS NOT "/", BUT "" (empty string)
  2. I usually add optional "/" at the end of each route configuration, just in case
  3. I also usually add default action handler at the end of configuration

So my routes configuration would be like:

routes = {
    '': 'home',
    'pageb(/)': 'actionB', // so /pageb or /pageb/ will call the same function
    'pagec(/)': 'actionC', // so /pagec or /pagec/ will call the same function
    '*action': 'defaultAction' // you can use it to render 404, or call home function
}

Hope this help

Upvotes: 4

tkone
tkone

Reputation: 22728

You can either set "*path": "home" as your last route which will make it a default route or set "" (instead of "/")as your first route (which means root directory)

Upvotes: 17

Related Questions