Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 645
Boone Avatar asked Feb 15 '23 11:02

Boone


1 Answers

Try this:

ko.toJS(currentOrder);

or

ko.toJSON(currentOrder);

Knockout docs here.

like image 119
oddurad Avatar answered Apr 30 '23 03:04

oddurad