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

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

问题描述

关于backbone.js'save同步.

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

  1. 调用保存/同步函数后,主干希望服务器得到什么类型的响应?收到回复后是否需要任何特定的后处理?

  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?

backbone 如何知道模型是否已经存在于服务器上?从文档中我看到每个模型都有一个 isNew 来检查是否有ID".所以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) 首先创建并同步模型,

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 方法.对于模型,解析方法必须使用一个属性散列来响应,该散列将在对模型的设置"调用中使用.对于集合, parse 方法必须使用代表要由集合维护的模型的属性散列数组进行响应.在每种情况下,都有使用原始响应对象的默认实现.如果您的响应没有返回可用的哈希值,那么您需要提供自己的解析方法来满足您的需求.

  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.

您关于 isNew 如何工作的陈述是正确的.您的服务器响应必须提供某种 id,您可以在 parse 方法中使用或转换为模型上的 id 属性.如果您的服务器响应没有将对象 id 称为名为id"的属性,则需要进行转换.我认为您的整个第二个问题取决于您对 parse 方法的正确理解.此方法必须返回一个属性哈希,其中还包括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天全站免登陆