I have built a simple user model in Backbone
define([
'underscore',
'backbone'
], function(_, Backbone) {
var UserModel = Backbone.Model.extend({
urlRoot: '/api/user',
idAttribute: '_id',
defaults: {
userName: '',
password: ''
personalDetails: {
title: '',
name: {
firstName: '',
lastName: ''
},
gender: '',
dob: ''
}
}
});
return UserModel;
});
Calling save() on the model will issue a PUT request to /api/user/:id which is fine.
However, in the front end I want 3 forms - updatePersonalDetails, updateUserName, updatePassword.
With my current implementation these will all use the same model and all save() to the same endpoint /api/user/:id. My problem is that I wont know which form the user posted.
Whats the best solution to this issue whilst still conforming to RESTful standards? Having 3 separate Models - UserDetailsModel, UserPasswordModel etc? Or to just do a bulk UPDATE on the whole model regardless of which form the user posted.
Allot been discussed regarding model separation vs model BULK in the question comments, I have nothing to add there.
Regarding your need to understand the UPDATE request form source, you can use query string parameters:
Just POST /api/user/1?form=my_special_form
In backbone you just need to save the model with url option
model.save({}, {
url: model.url() + "?form=my_special_form"
});