Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a Backbone.js 'Model' expect back from the server after a call to Save()?

What does a Backbone.js 'Model' expect back from the server after a call to Save()?

The reason I ask is because I had an issue with the model getting cleared after a call to Save(). I fiddled around with the model's parse() method. I found the returning an empty object or null did not produce this behavior. However, the server was returning some JSON as a "success" message and this information seemed to be overwriting the model:

{
    message: 'SUCCESS'
}

What is the "correct" way for the server to respond to a Save() request from a Backbone model?

Thanks!

like image 657
Chris Dutrow Avatar asked Dec 08 '22 21:12

Chris Dutrow


1 Answers

The server should responde with an HTTP status of 200, most likely, and should return any data that the server generates or updates for the model. Typically, this is only the model's server generated id field. Any fields that are returned from the server will be applied to the model. So if you send this to the server:


{
  foo: "bar"
}

and the server response with this:


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

Your model will end up looking like this:


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

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

like image 153
Derick Bailey Avatar answered Feb 01 '23 22:02

Derick Bailey