0

Hi I want to make my root url have a :id segment like below which should be passed as param to every route that matches it

Backbone.history.start({ pushState: false, root: "/orders/:id/items"})

routes: {
    "": "item"
},

item: function(id) {
  alert(id);
}

but right now I am getting id as undefined, even though it is part of root url.

Please help me

Peru
  • 383
  • 2
  • 13
  • Does your website have a server language? It is only javascript based website? Why not write the real value of the ID from the server side? – Claudiu Hojda Aug 06 '12 at 14:27
  • It's Ruby on Rails application with Orders table has_many association with items table. – Peru Aug 06 '12 at 14:52

1 Answers1

1

Well, you can't have a parameter segment in your root url in Backbone.History.

Some source code from Backbone.History.getFragment that is used to get the url-fragment to match routes to and extract variables from (I laid it out a bit differently)

if (!fragment.indexOf(this.options.root)) { 
  fragment = fragment.substr(this.options.root.length);
}

This piece of code is where every single Router.navigate call ends up to (unless { trigger: false } is applied). So what happens that if the fragment contains your root url defined with the start-function, the fragment will be replaced with a substring of the original fragment that has the root conveniently left out. So your root url won't be even taken into consideration when the variables are picked away from the fragment.

so there is NO way to get variables into the root URL, because, by definition, the root url is just a pointer for Backbone.History in case your application is not served from the root url of your domain. Here's the quote:

If your application is not being served from the root url / of your domain, be sure to tell History where the root really is...

So if your application is not being served from /orders/12345/items and /orders/78342/items then I suggest that you don't try to tell your router that it is. Instead, serve it from / and then set up the routes normally:

routes: {
  'orders/:id/items': 'item'
}

Hope this clears things up!

jakee
  • 18,486
  • 3
  • 37
  • 42
  • Hi @jakee, Thanks a lot for the quick reply but my application is actually served from /orders/12345/items {i.e /orders/:id/items } and I need the :id to access that order details. Please guide me how to handle this case – Peru Aug 06 '12 at 13:49
  • so you mean you have a folder `orders` with n amount of folders `:id` and within each of those n-folders you have a folder `items` which contains a copy of your application? – jakee Aug 06 '12 at 14:40
  • I am using a rails app and the path here refers to database tables and not folders. Orders table has_many association with items table. – Peru Aug 06 '12 at 14:51
  • try removing the root from the `start`-call and using the route i provided in the end of my answer. Generally my intuition tells me that mixing rails with one-page-app routing isn't such a good idea and can lead to a confusing application. At least my understanding is that Rails is supposed to be your router. – jakee Aug 06 '12 at 14:58
  • I agree with @jakee in all his answers, but like I said in my other comment, you can render the page with the `ID` already being a static value on your backbone page app – Claudiu Hojda Aug 06 '12 at 15:04