主干 - 创建集合多模型 - 服务器端 [英] Backbone - Create Multiple Models in Collection - serverside

查看:196
本文介绍了主干 - 创建集合多模型 - 服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供了一个形式用户上传他们自己的数据。我使用Ajax的形式提交(以上传CSV一种全行),然后分析数据以创建模型的软件。

I provide a form for users to upload their own data. I use ajax-form-submit and then parse the data to create numerous models (one per row in uploaded csv).

现在,我想创建模型到predefined集合。

Now, I want to create models into a predefined collection.

我可以使用添加这需要的车型阵列但不幸的是,它并没有在服务器端发送PUSH。我知道我可以遍历并创建.create每个模型,但让我们说我有10K的机型,它会造成10K电话。听起来很不合理的。我错过了什么?

I can use add which takes an array of models but unfortunately, it does not send PUSH at server side. I know I can iterate and create .create for each model but let's say I have 10k models, it would create 10k calls. Sounds unreasonable. Did I miss anything?

另一种方法是在服务器接受多个模型,并使用阿贾克斯呼叫,然后手动添加到集合UI渲染。

The other way is to accept multiple models at server and use .ajax calls and then add manually to the collection for UI rendering.

寻找最佳路线。谢谢你。

Looking for the best route. Thanks.

推荐答案

骨干和REST根本不覆盖所有真实使用情况下,如你批量创建实例。他们也不具备批量正式的模式删除,这也是极为普遍。我感到莫名其妙,为什么他们拒绝解决这些非常普遍的用例,但在任何情况下,你留下你自己良好的判断力在这里。所以我建议增加一个 bulkSave 导入方法到您的收藏。这应该与你的CSV格式数据发送POST AJAX请求到服务器,服务器端应保存的信息,如果一切顺利的话,返回新创建的模型的JSON阵列。你收集应采取JSON数组中的POST响应,并将其传递给重置(和解析,以及如果需要特殊解析)。

Backbone and REST simply do not cover all real-world use cases such as your bulk create example. Nor do they have an official pattern for bulk delete, which is also extremely common. I am baffled as to why they refuse to address these extremely common use cases, but in any case, you're left to your own good judgement here. So I would suggest adding a bulkSave or import method to your collection. That should send an AJAX POST request with your CSV form data to the server, the server should save the info and if all goes well, return a JSON array of the newly-created models. You collection should take that JSON array in the POST response and pass it to reset (and parse as well if you need special parsing).

绝对不做每个模型(在你的CSV行)POST请求,特别是如果你计划有10K车型。但是,要清楚,它不会是完全可怕做模式为几十款,如果你的用户界面显示实时的进度和错误处理对每个记录的基础上(65 23保存,例如)。

Definitely don't do a POST request for each model (row in your CSV), especially if you plan on having 10K models. However, to be clear, it wouldn't be completely terrible to do that pattern for a few dozen models if your UI shows real-time progress and error handling on a per-record basis (23 of 65 saved, for example).

这篇关于主干 - 创建集合多模型 - 服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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