Basic user model with multiple update paths

I 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 produce a PUT request to /api/user/:id , which will be fine.

However in front I want 3 forms - updatePersonalDetails , updateUserName , updatePassword .

In my current implementation, they will all use the same model and all save() for the same endpoint /api/user/:id . My problem is that I donโ€™t know what form the user posted.

What is the best solution to this problem, although RESTful? Having 3 separate models - UserDetailsModel , UserPasswordModel , etc.? Or just do a voluminous UPDATE on the entire model, no matter what shape it posted.

+4
source share
1 answer

In questioning comments, the division of the model into the BULK model was discussed, I have nothing to add there.

As for your need to understand the source of the UPDATE query form, you can use the query string parameters:

Just POST / api / user / 1? form = my_special_form

In the spine, you just need to save the model with the url parameter

 model.save({}, { url: model.url() + "?form=my_special_form" }); 
+2
source

Source: https://habr.com/ru/post/1488044/


All Articles