Boone
Boone

Reputation: 1054

Convert Knockout object with Observables to regular objects

I have an object like below.

var order = function (data) {
    this.OrderId = data.Id;
    this.CustomerName = ko.observable(data.CustomerName);
    this.CustomerAddress = ko.observable(data.CustomerAddress);
    this.CustomerPhone = ko.observable(data.CustomerPhone);
    this.TotalPrice = ko.observable(data.TotalPrice);
    this.Cancelled = ko.observable(data.Cancelled);
    this.Pizzas = ko.observableArray();
};

In my VM:

var currentOrder = new model.Order({});

When this object gets modified from the UI, everything works fine. My problem comes in when I want to pass this object to my data layer to get saved. It comes in like:

Chrome console.log output

Obviously, I can't pass this to my data layer. Is there an easy way to strip this complex object of all the knockout stuff without manually writing a big mapper?

Upvotes: 4

Views: 4950

Answers (1)

oddurad
oddurad

Reputation: 417

Try this:

ko.toJS(currentOrder);

or

ko.toJSON(currentOrder);

Knockout docs here.

Upvotes: 8

Related Questions