javascript - Store a function in IndexedDb datastore -


is possible in way store function in indexeddb datastore? have done searching , found nothing on data types indexeddb supports. tried adding function , instance of function object store follows, didn't work.

var myclass = function () {     self = this;     self.name = 'sam';     self.hello = function () {         console.log('hello ' + self.name);     }; }  var transaction = db.transaction(['codeobject'], 'readwrite'); var store = transaction.objectstore('codeobject'); var request = store.put({ 'classname': 'someclass', 'object': myclass }); 

and have tried.

var request = store.put({ 'classname': someclass', 'object': new myclass() }); 

i store class in object db. if had store type of blob , serialize function on way out.

thank you

if question right, want serialize entire object including functions?

this possible do, need make possible serialize functions. more information it, see my blogpost 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 object json string including functions var objectstring = json.stringify(jsonobj, serialize);  // turns object string in json object including functions. var object = json.parse(objectstring , deserialize); 

the objectstring can saved indexeddb.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -