Backbone REST request to add users to project? Backbone REST request to add users to project? codeigniter codeigniter

Backbone REST request to add users to project?


It sounds as though the User can have many Projects and a Project can have many Users. In that case managing that relationship is best done through a join table that you could name something like Participants which would have a user_id and a project_id.

Then you can choose whether to manage Participants through a User or through a Project. To me it would make sense to manage your Projects Participants, and because you are creating a new Participant it is a POST.

POST /projects/:project_id/participants

The payload would contain the user_id.


If project id is not part of the user model, then that implies an array of user ids is part of the project model ... right? So I think this would be appropriate to add a user to a project:

PUT /projects/(projectid)/users/(userid)

However, maybe you don't need to support those URLs if they're not needed by the consumers of the service (ie, Backbone); maybe you just want a single endpoint to update projects:

PUT /projects/(projectid)

and your Ajax post call would include a list of users as part of the "update this project" request.

You might want to use either form of the URL from Backbone, depending on the UI. The first form of the URL is really only useful for requests like "add user x to project y" (say when an admin clicks clicks "add this user to the current project", or whatever).

The second form of the URL is what you might use for updating a project wholesale, which could include the list of users, name of the project, etc; that is the "edit project" view.

var ProjectModel = Backbone.Model.extend({    defaults: {        "project_name": "",        "users: [0]    },    urlRoot: "/myrestapi/projects",    url: function() {        return this.urlRoot + "/" + this.cid;    }});

A Backbone save request on this model would trigger a (POST? PUT?) to the URL:

/myrestapi/projects/(cid)

With the post data representing the model state of the project:

{ "project_name": "foo", "users": [15, 18, 42] }

It's then up to your REST service to trigger the updates to your server's data store ...