Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suitable data structures for saving files in localStorage (HTML5)?

It is nice when there isn't a DB to maintain and users to authenticate. My professor has asked me to convert a recent research project of his that uses Bespin and calculates errors made by users in a code editor as part of his research.

The goal is to convert from MySQL to using HTML5 localStorage completely. Doesn't seem so hard to do, even though digging in his code might take some time.

Question:
I need to store files and state (last placement of cursor and active file). I have already done so by implementing the recommendations in another stackoverflow thread. But would like your input considering how to structure the content to use.

My current solution > Hashmap like solution with javascript objects:

files = {};
// later, saving
files[fileName] = data;

And then storing in localStorage using some recommendations

localStorage.setObject("files", files);
// Note that setObject(key, data) does not exist but is added
// using Storage.prototype.setObject = function() {...

Currently I'm also considering using some type of numeric id. So that names can be changed without any hassle renaming the key in the hashmap. What is your opinion on the way it is solved and would you do it any differently?

like image 512
JeroenEijkhof Avatar asked May 10 '10 02:05

JeroenEijkhof


People also ask

What data type can localStorage save in the browser?

Like cookies, LocalStorage can only store string data for its keys and values. The datastore is only accessible to JavaScript within that domain. Note: Each domain has access to its LocalStorage datastore.

Does HTML5 have local storage?

Local storage is part of the HTML5 Web Storage API and it allows you to store data in the browser. Unlike cookies, data stored using local storage isn't sent back to the server. All data stays on the client, and you can currently store from 2MB to 10MB.

What is local storage in HTML5?

Local storage is mainly used to store and retrieve data in HTML pages from the same domain. Even after restarting a browser, the data can be recovered from all windows in the same domain. This type of storage offers numerous options for Web apps.


1 Answers

Currently I have decided after doing some reading and research that the best way to go is to store everything in objects using objects literals:

var files = {};
// Add loads of data

And then storing them using JSON.stringify():

localStorage.setItem('myFiles', JSON.stringify(files));

This makes good practice because it store large amounts of information that is easy to store and access. Also it prevents from getting confusing when to use the simple way of adding key-value pair information in localStorage and when to use the localStorage.setItem(key, value) function

like image 141
JeroenEijkhof Avatar answered Oct 22 '22 14:10

JeroenEijkhof