简体   繁体   English

我如何将Backbone.js集合保存到数据库

[英]How I can save Backbone.js collections to database

I have the Backbone's collection with some models, added to it. 我添加了一些模型的Backbone集合。 How I can save this collections database. 如何保存此收藏数据库。 I want to have the possibility to recreate this collection by using data from database. 我想有可能通过使用数据库中的数据来重新创建此集合。 Could you help me? 你可以帮帮我吗? How can I do it? 我该怎么做?

Collections can be output as an array of JSON objects that you can iterate over to save any information you would like. 集合可以作为JSON对象数组输出,您可以对其进行迭代以保存所需的任何信息。 If you are looking to re-create the list, the id's of the obects themselves may well be suffecient 如果您要重新创建列表,对象的ID可能就足够了

You can recreate the collection by using fetch() or reset(). 您可以使用fetch()或reset()重新创建集合。 You can add by calling create on collection. 您可以通过在集合上调用create来添加。

http://documentcloud.github.com/backbone/#Collection-create http://documentcloud.github.com/backbone/#Collection-create

These are basic infos and they are all on backbone website. 这些是基本信息,它们都在骨干网站上。

The short answer is that Backbone.js doesn't cover this at all, it's up to you. 简短的答案是Backbone.js根本不解决这个问题,这取决于您。

The more detailed answer is that Backbone is designed around (or at least, works well with) the premises of a RESTful API: you use one of many back-end programming language (or, more likely, a framework in one of those languages) in order to represent your resources. 更详细的答案是,Backbone是围绕RESTful API的前提设计的(或至少与之兼容):您使用多种后端编程语言中的一种(或更可能是使用其中一种语言的框架)为了代表您的资源。 In all likelihood you'll be outputting JSON from that back end, which is what Backbone speaks natively. 您很有可能会从该后端输出JSON,这就是Backbone所说的。

All of which is to say, you've still got your work cut out for your if you want to have a database-backed Backbone app. 综上所述,如果您想拥有一个数据库支持的Backbone应用程序,那么您的工作就仍然可以完成。

On the other hand, if you're working on something for yourself that won't have enormous amounts of data, you can rely on the localStorage plugin to store your data in your browser. 另一方面,如果您正在为自己处理一些不会有大量数据的工作,则可以依靠localStorage插件将数据存储在浏览器中。 It's a pretty great way to get started playing with Backbone and just concentrating on the front end functionality. 这是开始使用Backbone并专注于前端功能的绝佳方法。

My own plan is to use localStorage until I'm happy with the way my app is handling data and/or it starts to become too slow. 我自己的计划是使用localStorage直到我对我的应用程序处理数据的方式感到满意和/或开始变得太慢为止。 Because localStorage can be a little volatile (what happens to it if you upgrade your browser? I don't know, and I don't want to), I also periodically dump all that data into a JSON document and store it in my filesystem. 因为localStorage可能有点不稳定(如果升级浏览器会发生什么?我不知道,我也不想这么做),所以我也会定期将所有数据转储到JSON文档中并将其存储在我的文件系统中。

Obviously this is only a useful approach for developing locally and learning Backbone itself. 显然,这只是用于本地开发和学习Backbone本身的有用方法。 Inevitably you (and I!) will have to bite the bullet and choose a back end. 不可避免地,您(和我!)将不得不硬着头皮选择一个后端。 Rails seems to be popular for this purpose, and there are many solutions in PHP . 为此,Rails似乎很流行, PHP中有许多解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM