Reputation: 6657
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?
Upvotes: 0
Views: 1007
Reputation:
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.
Upvotes: 1
Reputation: 618
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.
Upvotes: 1
Reputation: 7426
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
Upvotes: 1