How to stringify an Observable/ObservableArray


#1

json can be used to stringify and parse regular objects, which comes in when saving and loading objects.
Stringifying and parsing an observable is possible, but the observable behaviour is lost.

var test=observable.fromObject({'item':true} )

var clone=JSON.parse(JSON.stringify(test))
console.log(clone.get(‘item’)

In the above case console.log(clone.item) results true and console.log(clone.get(‘item’) raises an error.

Question: what is the way to stringify and parse an Observable/ObservableArray, such that the result of the parse is an Observable/ObservableArray.

Workaround
I could think of the following solution:

var test=observable.fromObject({'item':true} )

var clone=observable.Observable(JSON.parse(JSON.stringify(test._map)))

In this case console.log(clone.get(‘item’) returns true.I do not like this solution because it uses ._map which should be a hidden structure. Besides it does not work if the observable has nested observables (subobjects which are observables). So my question is, does Observable have a stringify/parse function to do the job?


#2

Try JSON.stringify(test._map)


#3

manojdcoder, the solution you propose I described above under workaround. As I commented in the workaround, It doesn’t work with nested observables. I can’t imagine there isn’t a more generic solution?!


#4

The more generic solution could be, keeping reference of the original object. If you expect more advanced data binding, templating etc., I would say you must choose Angular.