Your custom collection URL should be set to / user. After this set, your models should use this URL to do their magic. I believe (not quite positive) that if the model is in the collection, calling the url method will return / user /: id. This way all your typical REST-ish functions will be used in '/ user /: id'. If you are trying to do something with relationships (the user has a lot of documents), itβs like washing and repeating. So, for your collection of documents (which robs the user of the right?) You must set the URL "user_instance.url / documents".
To show a one-to-many relationship with the base model, you would do something like this (upgrade to base line 0.5.1 for urlRoot):
var User = Backbone.Model.extend({ initialize: function() { // note, you are passing the function url. This is important if you are // creating a new user that not been sync'd to the server yet. If you // did something like: {user_url: this.url()} it wouldn't contain the id // yet... and any sync through docs would fail... even if you sync'd the // user model! this.docs = new Docs([], {user_url: this.url}); }, urlRoot: '/user' }); var Doc = Backbone.Model.extend(); var Docs = Backbone.Collection.extend({ initialize: function(models, args) { this.url = function() { args.user_url() + '/documents'; }; } }); var user = new User([{id: 1234}]); user.docs.fetch({ success: function() { alert('win') });
source share