I’m trying to store variables in localStorage on my mobile website so if the user leaves, their state is not lost. In order to do that I store all important data as JSON in localStorage. However, some parts of the data are Parse objects (Backbone models) and have a toJSON function. In order to unobtrusively store the data, I need to remove the toJSON so that the object is stored as-is, and then re-attach it. However, my code isn’t working:
var toJSON = null;
if(obj && obj.toJSON) {
toJSON = obj.toJSON;
delete obj.toJSON;
}
window.localStorage[localStorageIndex] = JSON.stringify(obj);
if(toJSON)
obj.toJSON = toJSON;
Any ideas why?
Thanks.
I learned not to store heavy objects using localStorage…