Knockout.js mapping database data to multiple objects

179 Views Asked by At

I am trying to load data into two javascript objects I created using Knockout.js but I am not really sure how to go about it.

Currently, I have the following objects:

function Session(data) {
    this.id = data.id;
    this.class_id = data.class_id;
    this.user_id = data.user_id; // NEEDS TO BE MADE UP OF USER OBJECTS
    this.stored_elapsed = data.stored_elapsed;
}

function User(data) {
    this.id = data.id;
    this.first = data.first;
    this.last = data.last;
}

Next, I have my view model to load the data:

function SessionViewModel() {
    // Data
    var self = this;

    self.sessions = ko.observableArray([]);

    // Load data
    $.getJSON("/sessions/getList", function(allData) {
        var mappedSessions = $.map(allData, function(item) {
            return new Session(item) // NEED TO MAP USER OBJECTS INTO SESSIONS
        });
        self.sessions(mappedSessions)
    });
}

ko.applyBindings(new SessionViewModel());

That works to create session objects, but how can I also create the user objects that are part of the session objects?

0

There are 0 best solutions below