简体   繁体   中英

How I can save Backbone.js collections to database

I have the Backbone's collection with some models, added to it. 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. If you are looking to re-create the list, the id's of the obects themselves may well be suffecient

You can recreate the collection by using fetch() or reset(). You can add by calling create on collection.

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.

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. In all likelihood you'll be outputting JSON from that back end, which is what Backbone speaks natively.

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.

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. It's a pretty great way to get started playing with Backbone and just concentrating on the front end functionality.

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. 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.

Obviously this is only a useful approach for developing locally and learning Backbone itself. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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