应该如何服务器骨干同步响应 [英] How should server respond to backbone sync

查看:75
本文介绍了应该如何服务器骨干同步响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Backbone.js的保存并的同步

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


  1. 调用save /同步功能后,确实骨干从服务器期待什么类型的反应呢?在接收到响应后需要任何特定的后期处理?

  1. 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?

如何骨干知道模型是否已经存在服务器上?从文档我看到每个模型有一个是否新款用于检查是否有身份证。因此,ID是从服务器的ID,而CID是骨干,为每个模型创建。正确?如果是这样,什么是服务器的一般步骤,告知骨干模型的id是什么,什么时候:

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)接着当模特是牵强,

b) subsequently when the model is fetched,

c)或当模型被填充在页面加载?

c) or when the model is populated on page load?

感谢您的帮助。

推荐答案

回答您的问题:


  1. 当你得到响应从服务器返回的响应数据通过解析方法。对于模型,解析方法必须与响应属性的哈希将在一个设定被用来调用模型。对于收藏,parse方法必须以属性的数组回应散列车型重新presentative由集合进行维护。在每一种情况下,存在其中使用原始响应对象的默认实现。如果你的反应不会返回可用的哈希值,那么你需要提供自己的解析方法,你想要做什么。

  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.

您是如何是否新款作品在你的说法是正确的。您的服务器的响应必须提供某种形式的ID,你要么使用或改造成一个id属性在parse方法的模型。将需要转换,如果你的服务器响应不调用对象ID为称为id属性。我觉得你的整个第二个问题取决于解析方法的正确理解。此方法必须返回一个属性哈希其中还包括id属性。

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.

这篇关于应该如何服务器骨干同步响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆