与后端同步集合更改 [英] Sync collection changes with the backend

查看:83
本文介绍了与后端同步集合更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Backbone.js,并且有一些模型集合.检索此集合并将其显示在前端.在前端,我希望用户删除新模型并将其添加到集合中.

I use Backbone.js and I have a collections of models. This collection is retrieved and displayed on the front-end. On the front-end, I want the user to remove and add new models to the collection.

当用户完成操作并单击保存"时,我希望更新整个集合.这意味着在单击保存"时,将对收藏集进行同步(以某种方式).保存添加的模型,并删除删除的模型.

When the user is finished and he clicked "save", I want the entire collection to be updated. Meaning that when clicking 'save', the collection is synced (somehow). Added models are saved and removed models are deleted.

如果我通过删除和添加模型来操纵集合,然后使用ex:

If I manipulate the collection by removing and adding models, and then use ex:

this.collection.sync()

它将删除并添加模型吗?

Will it remove and add models?

推荐答案

至少有两种方法可以实现这一目标.

There are at least 2 ways to achieve this.

添加/更新模型时,请直接使用 .save 保存模型.模型已删除,请在其上调用 .destroy .

When adding/updating a model, save the model directly with .save and when a model is removed, call .destroy on it.

该集合还具有一个 .create函数,该函数会向其中添加新模型并保存同时.

The collection also has a .create function which adds the new model to it and saves it at the same time.

在一个请求中做所有事情的最好的事情.

Best thing to do everything in one request.

并非总是如此.集合可能很大,而变化却很小,因此每次与服务器交换100个对象,而不是X个小的请求以添加,删除或更新列表中的模型.

Not always. The collection could be big and the changes rather small, so exchanging 100 objects with the server each time instead of X small requests to add, delete or update a model within the list.

专业人士

  • 可重复使用的端点以管理各个模型
  • 轻量数据传输,比发送整个集合要快(仅通过部分更新发送更改)
  • 每个动作可能的自定义行为
  • 轻松实现实时更新实施
  • Reusable endpoints to manage individual models
  • Light data transfer, faster than sending the whole collection (only changes are sent through partial updates)
  • Possible custom behaviour for each action
  • Easy real-time update implementation

缺点

  • 进行大量更改时有更多请求
  • 需要模型具有ID
  • 要管理的其他端点
  • 不适用于批量操作(一次保存所有模型)

不打算保存收藏集.而是将集合的models放入与API端点进行通信的模型中.该终结点应该期望有一个带有数组字段的对象,该字段可以用来替换收集服务器端.

Collections are not meant to be saved. Instead, put the models of the collection into a model which communicates with an API endpoint. This endpoint should expect an object with an array field, which can serve to replace the collection server-side.

var CollectionModel = Backbone.Model.extend({
    urlRoot: "collection/endpoint/"
});

var myModel = new CollectionModel();

// ...sometime later...

myModel.save({
    arrayAttribute: yourCollection.toJSON()
}, { patch: true });

专业人士

  • 一个端点;对API的调用始终相同
  • 易于实施;只是保存在一个地方
  • One endpoint; always the same call to the API
  • Easy to implement; just in one place where the save occurs

缺点

  • 所有模型均根据每次请求进行转移,无论更改如何
  • 如果收藏量很大,可能会变慢

收藏夹的.sync函数 Backbone.sync 的代理,如果没有正确的参数,则什么也不做.它仅在.fetch(第1055行 ),除非添加自定义行为,否则不得直接使用.

Collection's .sync function is only a proxy to Backbone.sync and do nothing without the correct parameters. It is only used internally within .fetch (line 1055) and isn't meant to be used directly, unless adding a custom behavior.

这篇关于与后端同步集合更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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