Ember数据批量保存到服务器 [英] Ember Data bulk saves to server

查看:157
本文介绍了Ember数据批量保存到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ember数据从版本快速移动到版本,并且保存数据的方法一直随之改变。现在版本为1.0.0-beta.8.2a68c63a,正确的方法是更新记录,然后执行record.save()来将PUT请求触发回服务器。使用我当前的应用程序,我一次更新多个记录,可能会将50多个PUT ajax请求返回到服务器。我们关心性能和效率问题,并没有找到任何批量请求的文档。如果有的话,我们发现很多在线的其他人正在寻找同样的事情,并没有找到一个很好的解决方案。

Ember Data is moving fast from version to version and the method for saving data has been changing along with it. Right now with version 1.0.0-beta.8.2a68c63a the proper method is to update a record and then do a record.save() to trigger a PUT request back to the server. With my current app I'm updating multiple records at once and that could involve 50+ PUT ajax requests back to the server. We're concerned about performance and efficiency issues and haven't found any documentation for doing batch requests. If anything, we've found a lot of other people online that are looking to do the same thing and haven't found a good solution.

现在我是查看手动序列化这些对象并将其保存回服务器,我以为是Ember Data的整个点。所以也许最好不要使用Ember Data,并手动编码出CRUD请求,并使自己的数据层可以处理所有这些,并且只需使用ArrayControllers和ObjectController来保存数据并绑定到Handlebars模板。似乎这样做的好处是,它将以我需要的方式(批处理请求)工作,并且代码将不会与未来版本的Ember Data中断。任何想法或解决方案?

Right now I'm looking into manually serializing these objects and saving them back to the server, which I thought was the entire point of Ember Data. So maybe it's in my best interest to not use Ember Data at all and manually code out CRUD requests and make my own data layer for handling all of this and just use ArrayControllers and ObjectControllers to save the data in and bind to the Handlebars templates. It seems like the benefits to this would be that it would work the way I need it to (batch requests) and the code won't break with future versions of Ember Data. Any thoughts or solutions?

推荐答案

您可以使用 DS.EmbeddedRecordsMixin 。如果您的Page对象有多个块(例如,基于您的评论),则 page-serializer.js 将如下所示:

You can do it with the DS.EmbeddedRecordsMixin. If your Page object has many Blocks (for example, based on your comment), then page-serializer.js would look like:

import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    blocks: { serialize: 'records' }
  }
});

无论何时保存页面记录,所有关联的块记录将被包含在该一个PUT请求中。

Whenever you save a page record all of its associated blocks records will be included in that one PUT request.

请参阅文档这里

这篇关于Ember数据批量保存到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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