I have the following routes defined in rails:
resources :accounts do
resources :transactions
end
This results in urls like:
/accounts/123/transactions/1
Is there an easy way to map this to a backbone model set up?
I have the following routes defined in rails:
resources :accounts do
resources :transactions
end
This results in urls like:
/accounts/123/transactions/1
Is there an easy way to map this to a backbone model set up?
Turns out backbone quite easily supports this by nesting a collection in a model as follows:
This achieves exactly what I wanted.
You can read more about it here: http://documentcloud.github.com/backbone/#FAQ-nested
Just define the url of your model or (if you use one) your collection like so:
or dynamically:
Those models or all models of a collection that is configured this way will now generate it's backend urls accordingly.
Detailed info:
Model: http://documentcloud.github.com/backbone/#Model-url
Collection: http://documentcloud.github.com/backbone/#Collection-url
Backbone does not directly support the creating of nested urls. You must use a function to dynamically calculate the resulting url of your nested object. For example:
More information: http://documentcloud.github.com/backbone/#FAQ-nested
It might not be an easy way but I think the best way is to use url and set it to a function like this:
Or maybe in coffeescript (as it is backbone+rails):
Oh and you could do it more like this(surely with deeper nesting it's better):
AFAIK there is now url utility in backbone, and it's not much enough pain for me so that I would search for one in some other library :)