admin管理员组文章数量:1336568
Some newbie questions about backbone.js' save and sync.
After calling save/sync functions, what type of response does backbone expect from the server? Is any specific post-processing required after the response is received?
How does backbone know whether the model already exists on the server? From the documentation I see that each model has an isNew that checks for whether there is an "id". So "id" is the id from the server, whereas "cid" is what backbone creates for each model. Correct? If so, what are the general steps for the server to inform backbone what the id of a model is, when:
a) the model is first created and synced,
b) subsequently when the model is fetched,
c) or when the model is populated on page load?
Thanks for help.
Some newbie questions about backbone.js' save and sync.
After calling save/sync functions, what type of response does backbone expect from the server? Is any specific post-processing required after the response is received?
How does backbone know whether the model already exists on the server? From the documentation I see that each model has an isNew that checks for whether there is an "id". So "id" is the id from the server, whereas "cid" is what backbone creates for each model. Correct? If so, what are the general steps for the server to inform backbone what the id of a model is, when:
a) the model is first created and synced,
b) subsequently when the model is fetched,
c) or when the model is populated on page load?
Thanks for help.
Share Improve this question asked Jun 18, 2011 at 5:33 fortuneRicefortuneRice 4,20411 gold badges45 silver badges60 bronze badges 1- 3 I agree that the docs could say more about this. – UpTheCreek Commented Sep 21, 2011 at 20:09
1 Answer
Reset to default 9Answers to your questions:
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.
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.
本文标签: javascriptHow should server respond to backbone syncStack Overflow
版权声明:本文标题:javascript - How should server respond to backbone sync - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742411849a2469947.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论