What does Backbone.js 'Model' return from the server after calling Save ()?

What does Backbone.js 'Model' return from the server after calling Save() ?

The reason I ask is because I had a problem deleting the model after calling Save() . I was looking for a model parse() method. I found that the returned empty object or null did not cause this behavior. However, the server was returning some JSON as a success message, and this information seemed to rewrite the model:

 { message: 'SUCCESS' } 

What is the โ€œrightโ€ way for a server to respond to a Save() request from a Backbone model ?

Thanks!

+4
source share
2 answers

The server should respond with an HTTP status of 200, most likely, and should return any data that the server generates or updates for the model. As a rule, this is only the id type generated by the server. Any fields returned from the server will be applied to the model. Therefore, if you send this to the server:

 { foo: "bar" } 

and server response with this:

 { id: 1, // or some other server generated ID, from a database, etc. message: "SUCCESS" } 

Your model will look like this:

 { id: 1, foo: "bar", message: "SUCCESS" } 

If your model does not need any data updated from the server when calling the save method, you must return an empty object literal {} .

+10
source

Well, this question often appears here, and I already asked it at SO. It seems that adding this comment will not help the future researcher, therefore, there is a link to the answer and question

Getting started with backbonejs - what should the server return

+4
source

All Articles