0
votes

When I save an observable to IndexedDB, I get a DataCloneError.

E.g. using IDBWrapper: https://github.com/jensarps/IDBWrapper

store = new IDBStore({
    storeName: 'myStore',
    keyPath: 'Id',
    autoIncrement: true,
    onStoreReady: loadCallback
});

...

var myObservable = ko.observable("value");

...

store.put(myObservable, refreshCallback);

What would be the best way of working around this? Has anyone tackled this yet?

I have quite a complicated observableArray of object that contains observables and non-observables, so just grabbing the values out would be a bit repetitive.

UPDATE:

So far I have tried ko.toJS which is promising (http://knockoutjs.com/documentation/json-data.html) however because I am also using an isDirty flag with some of my observables (see here: http://www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html) this gets converted to a function so I'd need to deal with that manually anyway... I could to ko.toJSON so the whole thing gets stringified but then would it be harder to convert that back to the observable format compared to just manually setting each value?

So currently I am just doing this:

var myObservable = ko.observable("value")

...

var plainValue = myObservable();
store.put(plainValue, refreshCallback);

Only I have an observableArray of varying contents, so I have to do a lot of checks to see if values are observable or not, and if so then unwrap them and store them separately. Seems a bit messy. Is there a nicer way?

1
Show the code you're using to save the observable. - ebohlman

1 Answers

1
votes

DataCloneError occur when the object could not be serialized. I prefer plain old javascript object (JSON) to store in indexeddb. So, your solution is preferable. Generally I will have constructor for myObservable from JSON serialized object. It is clean and future proof. For some reason, if you want to change myObservable, you can still instantiate from old and new JSON.