admin管理员组

文章数量:1323330

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!

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!

Share asked Aug 25, 2012 at 2:24 Chris DutrowChris Dutrow 50.4k67 gold badges195 silver badges262 bronze badges 1
  • Thanks for asking this question exactly how I would have. – gfullam Commented Nov 26, 2014 at 19:46
Add a ment  | 

2 Answers 2

Reset to default 10

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.

Well this question often pop's out here and I have already asked it at SO. Seems adding this to ments is not going to help future questioner hence here is the link to answer and question

Getting started with backbonejs - what should a server return

本文标签: