Does anyone have a good solution for storing Parse.com objects into localStorage for Javascript projects?
The problem I have come across is that when converting the object to JSON, we lose the id on the object. This is because the id in Parse.com objects isn’t stored in the attributes.
Give me some hint.
Thanks in Advance.
Update
My little example
var Something = Parse.Object.extend("Something");
var something = new Something();
something.set("attribute1", "attribute1");
something.save(null, {
success: function(obj) {
something.fetch({
success: function(obj) {
console.log("object:"); console.log(obj);
console.log("json:"); console.log(JSON.stringify(obj));
},
})
},
})
Which give this output:
object: appmob.js:1563
d.hasOwnProperty.f
_changing: false
_escapedAttributes: Object
_existed: true
_hasData: true
_hashedJSON: Object
_opSetQueue: Array[1]
_pending: Object
_previousAttributes: Object
_saving: false
_serverData: Object
_silent: Object
attributes: Object
changed: Object
cid: "c0"
createdAt: Mon Sep 03 2012 15:43:50 GMT+1000 (EST)
id: "e419YJmhR5"
updatedAt: Mon Sep 03 2012 15:43:50 GMT+1000 (EST)
__proto__: c
** which has the id.
json:
{"attribute1":"attribute1","attribute2":"attribute2"}
** which doesn’t have the id.
This is related to this stackoverflow question
Backbone model .toJSON() doesn't render all attributes to JSON
So I was wondering what would be the simplest way to store the JSON string into localstorage.
Ok, so from what I can tell (see comments above), the native JS API for Parse assumes you only want to serialize the attributes (ie core data) of objects and NOT the actual metadata, including ID, created, etc. Since they really want you to store stuff ONLY on their system, this makes sense. However, you can write your own code to handle this yourself. For example, this method below grabs two metadata keys (id and createdAt, there IS more, but I was just testing) and creates a key called ‘data’ for the core attributes.
I tested this a sample result object and it worked ok. Again though you probably want to add the other metadata keys for completeness. (Or maybe you only care about id.)