I’ve been tinkering with Backbone.js for some time now. I’ve been using localStorage for storing most of my app data. I was wondering if would be possible for me to export my collection to a plain text and have it imported back again. i.e I want to create a backup kind of feature where when the data is exported, an HTML textbox is displayed with the ‘plain text’ version of my collection and when I need to import back the data, I just paste it into an empty textbox and it re-creates the collection.
Is this possible, any tips on how to achieve this? I was thinking about exporting it to JSON format and importing it back. Is that feasible?
p.s. I know there is no code here, but any idea would be much welcomed.
You can use
toJSONto serialize the collection. (Actually Backbone.js mentions this as a primary use forcollection.toJSON()). UseJSON.stringifyto convert the JSON object to a string.To restore the collection, reverse the steps:
JSON.parseto convert the raw string to a JSON objectcollection.resetto convert the JSON object to a collectionHere’s a working example.