Backbone and Rails Nested Routes

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?

Answers


Turns out backbone quite easily supports this by nesting a collection in a model as follows:

var Account = Backbone.Model.extend({

  initialize: function() {
    this.transactions = new TransactionsCollection;
    this.transactions.url = '/account/' + this.id + '/transactions';
    this.transactions.bind("reset", this.updateCounts);
  },
});

This achieves exactly what I wanted.

You can read more about it here: http://documentcloud.github.com/backbone/#FAQ-nested


Need Your Help

How to access an already-running Application Context from a Sync Adapter service in Android?

service sync android

I have an app that consists of several activities, and I use the Application Context (entended from the Application Class, and I made it persistent) to share data and objects between all the activi...