How should server respond to backbone sync

前端 未结 1 783
醉酒成梦
醉酒成梦 2020-12-31 08:51

Some newbie questions about backbone.js\' save and sync.

  1. After calling save/sync functions, what type of response does backbone expect from the server? Is a

相关标签:
1条回答
  • 2020-12-31 09:21

    Answers to your questions:

    1. When you get a response back from the server, the response data goes through the parse method. For models, the parse method must respond with an attributes hash which will be used in a 'set' call to the model. For collections, the parse method must respond with an array of attributes hashes representative of the models to be maintained by the collection. In each case, there are default implementations which use the raw response object. If your response does not return usable hashes, then you need to provide your own parse method which does what you want.

    2. You are correct on your statement about how isNew works. Your server response must provide some sort of id that you either use or transform into an id attribute on the model in the parse method. The transform would be required if your server response does not call the object id as an attribute called 'id'. I think your entire second question hinges on your proper understanding of the parse method. This method must return an attributes hash which includes also the 'id' attribute.

    0 讨论(0)
提交回复
热议问题