jokul
jokul

Reputation: 1339

How to store complicated object (with knockout observables) locally?

I have a very simple chat protocol that is integrated with sending site-related data between clients:

var ThreadList = function(){
    var self = this;

    self.threads = ko.observableArray(); //Thread[]
    //more members
}

var Thread = function(){
    var self = this;

    self.messages = ko.observableArray(); //Message[]
    //more members
}

var Message = function(source){
    var self = this;

    self.header = ko.computed(function(){
        //logic
    }, self);

    self.body = ko.observable();
    //more members
}

A thread is a simple discussion thread between two members, and every thread contains several messages between these members. Multiple threads are contained with the ThreadList object.

How can I go about storing this information locally? Using localStorage would require a large amount of extra code to serialize every single member and their values.

Upvotes: 0

Views: 339

Answers (1)

Ashwin
Ashwin

Reputation: 262

If you are worried about the code for serializing the data, knockoutJS has a function ko.toJS(object). This would return the serialized data.

var serializedData = ko.toJS(ThreadList); 

That would return the serialized data.

Upvotes: 2

Related Questions