Backbone.sync澄清 [英] Backbone.sync clarification

查看:86
本文介绍了Backbone.sync澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读文档之后,这就是我对sync的理解.

After reading the docs, this is my understanding of sync.

我实例化一些Backbone.Model并调用Collection.create(). create()最终调用sync(),并且ModelPOST绑定到服务器.然后在相反的方向上有一个sync,这样客户端上的Model被赋予了id.

I instantiate some Backbone.Model and call Collection.create(). create() eventually calls sync() and the Model is POSTed to the server. Then there is a sync in the opposite direction such that the Model on the client is given an id.

此更新是否触发componentDidUpdate()?
注意: componentDidUpdate是ReactJS的东西,因此,如果这没有意义,那么问题就变成了客户端模型是否已更新并且视图是否已重新呈现?"

Does this update then trigger componentDidUpdate()?
Note: componentDidUpdate is a ReactJS thing, so if that doesn't make sense, the question reduces to "Is the client-side model updated and the view re-rendered?"

由于在我的componentDidUpdate()内部,我正在调用save()以使所有内容保持最新,因此随后会调用sync(),然后触发PUT请求(因为Model已经有id).

Since inside of my componentDidUpdate() I am making a call to save() to keep everything up to date, this subsequently makes a call to sync() which then fires a PUT request (because the Model already has an id).

我问,因为在我当前的应用程序中,创建TodoItem似乎会导致POST然后是PUT,而我发现这是多余的.也许是出于不相关的原因.

I'm asking, because in my current application, creating a TodoItem seems to result in a POST and then a PUT which I find redundant. Perhaps it is for an unrelated reason.

添加一个项目时,它实际上会触发两个POST,然后触发两个PUTS,但这是另一个问题.

It actually fires two POSTS and then two PUTS when adding one item, but that is another question.

推荐答案

第一次保存模型(没有ID的模型)将进行POST,然后进行PUT(更新).我认为您在何时使用create/add/save感到困惑:

The first time you save a model (one which doesn't have an id) it will make a POST, thereafter it will make a PUT (update). I think you are confusing when to use create/add/save:

  • 随时使用save将当前客户端集合/模型状态保存到服务器
  • 使用add将模型添加到集合(单个模型,模型数组或包含属性的对象数组,然后由集合创建它们)
  • 使用create作为创建模型的快捷方式,将其添加到集合中,并将集合同步到服务器.
  • Use save at any point to save the current client collection/model state to the server
  • Use add to add Model(s) to a collection (a single Model, an array of Models, or an array of objects which contain attributes and let the collection create them)
  • Use create as a shorthand for creating a model, adding it to the collection, and syncing the collection to the server.

我的猜测是您要在一次操作中调用createsave-您应该改用addsave,或者只是使用create.

My guess is that you are calling create and save in one operation - you should be using add and save instead, or just create.

视图不会自动为您更新,您将需要听取集合/模型上的更改或事件并自行更新视图-没有与componentDidUpdate等效的视图.例如:

The view will not automatically update for you, you will need to listen to changes or events on the collection/model and update the view yourself - there is no equivalent of componentDidUpdate. For example:

initialize: function() {
    this.listenTo(this.collection, 'sync', this.onCollectionSync);
},

onCollectionSync: function() {
    this.render();
}

这篇关于Backbone.sync澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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