Let's say that I have an application with two collections: Users and Tasks . There is a multi-valued connection between them - a task can be associated with any number of users and vice versa. What would be the best way to deal with the new association?
One approach is to assign a new set of tasks to each user:
Users = Backbone.Model.extend({ this.tasks = new Tasks; ... });
then do this.tasks.create() with the model (or copy) as the input file.
The problem is that the task must have an identifier. If the model has an identifier, Backbone wants to upgrade. Therefore, not something like this:
POST /users/156/tasks
You will get something like this:
PUT /users/156/tasks/15
This is not what you want. You can simply override the isNew() function for this model to force Backbone to perform POST, but this seems like a hack.
Another approach is to maintain a list of identifiers from both ends. For example, a J2ON User model might look like this:
{ username: "jsmith" name: "Joe Smith" tasks: [5, 15, 256] }
Just take the identifier from the model you want to add and make a PUT for the user.
The problem is that more data is sent over the wire and no specific changes are presented. The server should do more work to filter through the model, find out what has changed, and add / remove associations as necessary (the backend here is SQL, so we are not just dealing with built-in task lists or something else). It seems much better to request the specific association change that is needed.
Which approach is best suited? Is there any other alternative that I am not considering?
(Note: I would like to avoid Backbone extensions like Backbone-relational)