保存整个集合的最佳实践?

发布于 2024-10-17 12:36:50 字数 58 浏览 4 评论 0原文

假设我有一个集合,并且我对其许多模型进行了更改。使用单个 HTTP 请求保存所有更改的最佳方法是什么?

Say that I have a Collection and I've made changes to many of its Models. What's the best way to save all of the changes using a single HTTP request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北恋 2024-10-24 12:36:50

通常,REST 后端处理单个实例创建/更新。您需要更改它以接受对象数组。

也就是说,在客户端,您需要直接转到 Backbone.sync 函数,

Backbone.sync = function(method, model, options)

在这种情况下,您的模型应该是模型数组。该方法应该是“创建”或“保存”,并且选项采用与 jQuery ajax 调用相同类型的选项(错误、成功等)

Usually REST backends handle single instance creation/update. You would need to change that to accept an array of objects.

That said, on the client side, you would need to go directly to the Backbone.sync function

Backbone.sync = function(method, model, options)

In this case your model should be an array of model. The method should be "create" or "save" and the options take the same type of options as a jQuery ajax call (error, success, etc.)

深海不蓝 2024-10-24 12:36:50

我将在这里做错误的事情并引用维基百科关于正确的 RESTful 实践PUTexample.com/resources 应将整个集合替换为另一个集合。基于此,当我们需要支持同时编辑多个项目时,我们写了这个合约。

  1. 客户端发送 {"resources": [{resource1},{resource2}]}
  2. 服务器用来自客户端的新信息替换整个集合,并在持久化后返回信息: {"resources": [{"id":1,...},{"id":2,...}]}

我们在 Rails 中编写了合约的服务器部分,但这是客户端部分(在 CoffeeScript 中,抱歉!):

class ChildElementCollection extends Backbone.Collection
  initialize: ->
    @bind 'add', (model) -> model.set('parent_id', @parent.id)

  url: -> "#{@parent.url()}/resources" # let's say that @parent.url() == '/parent/1'
  save: ->
    response = Backbone.sync('update', @, url: @url(), contentType: 'application/json', data: JSON.stringify(children: @toJSON()))
    response.done (models) => @reset models.resources

我认为这比覆盖 Backbone.sync 更容易实现。对代码的一项评论是,我们的集合始终是子对象,这应该解释为什么每当将对象添加到集合中时代码都会设置“parent_id”,以及 URL 的根如何是父级的 URL。如果您有要修改的根级集合,则只需删除 @parent 业务即可。

I'm going to do the Wrong Thing here and quote Wikipedia regarding proper RESTful practices: a PUT to example.com/resources should replace the entire collection with another collection. Based on this, when we had to support editing multiple items simultaneously, we wrote up this contract.

  1. The client sends {"resources": [{resource1},{resource2}]}
  2. The server replaces the entire collection with the new information from the client, and returns the information after it's been persisted: {"resources": [{"id":1,...},{"id":2,...}]}

We wrote the server half of the contract in Rails, but here's the client half (in CoffeeScript, sorry!):

class ChildElementCollection extends Backbone.Collection
  initialize: ->
    @bind 'add', (model) -> model.set('parent_id', @parent.id)

  url: -> "#{@parent.url()}/resources" # let's say that @parent.url() == '/parent/1'
  save: ->
    response = Backbone.sync('update', @, url: @url(), contentType: 'application/json', data: JSON.stringify(children: @toJSON()))
    response.done (models) => @reset models.resources

I thought this was a lot easier to implement then overriding Backbone.sync. One comment on the code, our collections were always child objects, which should explain why the code sets a "parent_id" whenever an object is added to the collection, and how the root of the URL is the parent's URL. If you have root-level collections that you want to modify, then just remove the @parent business.

星星的軌跡 2024-10-24 12:36:50

您应该扩展 Backbone.Collection,为其提供一个 save() 方法来检查每个模型的 hasChanged() 方法。

然后它应该调用 Backbone.sync,您可能需要将其扩展为自定义同步函数。如果您确实使用自定义 Backbone.sync 函数,请务必在您的集合上设置它。

var CollectionSync = function(method, model, [options]) {
    // do similar things to Backbone.sync
}

var MyCollection = Backbone.Collection.extend({
    sync: CollectionSync,
    model: MyModel,
    getChanged: function() {
        // return a list of models that have changed by checking hasChanged()
    },
    save: function(attributes, options) {
        // do similar things as Model.save
    }
});

一种不同的方法(使用模型来表示集合)在这里:“如何”在 Backbone.js 中保存整个集合 - Backbone.sync 或 jQuery.ajax?

我也喜欢 https://stackoverflow.com/a/7986982/137067

You should extend Backbone.Collection, giving it a save() method that would check each of its models hasChanged().

Then it should call Backbone.sync, which you'll probably have to extend a little into a custom sync function. If you do use a custom Backbone.sync function, then be sure to set it on your collection.

var CollectionSync = function(method, model, [options]) {
    // do similar things to Backbone.sync
}

var MyCollection = Backbone.Collection.extend({
    sync: CollectionSync,
    model: MyModel,
    getChanged: function() {
        // return a list of models that have changed by checking hasChanged()
    },
    save: function(attributes, options) {
        // do similar things as Model.save
    }
});

A different approach (using a model to represent the collection) is here: "How" to save an entire collection in Backbone.js - Backbone.sync or jQuery.ajax?

I also like https://stackoverflow.com/a/7986982/137067

她比我温柔 2024-10-24 12:36:50

这段代码向集合原型添加了一个新方法,只是为了调用已更改的模型的 save 方法。它对我有用:

Backbone.Collection.prototype.saveAll = function(options) {
 return $.when.apply($, _.map(this.models, function(m) {
   return m.hasChanged() ? m.save(null, options).then(_.identity) : m;
 }));
};

要点链接: https://gist.github.com/julianitor/701c677279bac1529b88

This code adds a new method to the collection prototype just to call the save method of those models that had changed. It worked for me:

Backbone.Collection.prototype.saveAll = function(options) {
 return $.when.apply($, _.map(this.models, function(m) {
   return m.hasChanged() ? m.save(null, options).then(_.identity) : m;
 }));
};

Gist link: https://gist.github.com/julianitor/701c677279bac1529b88

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文