If I get your question right, what you want to do is serialize the entire object including functions?
This is possible to do, you only need to make it possible to serialize functions. For more information about it, see my blogpost about it.
function serialize(key, value) {
if (typeof value === 'function') {
return value.toString();
}
return value;
}
function deserialize(key, value) {
if (value && typeof value === "string" && value.substr(0, 8) == "function") {
var startBody = value.indexOf('{') + 1;
var endBody = value.lastIndexOf('}');
var startArgs = value.indexOf('(') + 1;
var endArgs = value.indexOf(')');
return new Function(value.substring(startArgs, endArgs), value.substring(startBody, endBody));
}
return value;
}
var jsonObj = {
func: function(){ return ""; }
}
// Turns an object into a json string including functions
var objectstring = JSON.stringify(jsonObj, serialize);
// Turns an object string in JSON into an object including functions.
var object = JSON.parse(objectstring , deserialize);
The objectstring you get can then be saved into the indexeddb.