Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockoutjs Unwrapping ko.observableArray recursively

Tags:

knockout.js

I have javascript array in this format:

omega.franchiseInfo.rawdata = [{
                Id: "Main",
                Title: "Main",
                Type: "main",
                items: [{
                    Id: "Menu1",
                    Title: "Menu1",
                    Type: "menu",
                    items: [{
                        Id: "Menu1",
                        Title: "Menu1",
                        Type: "menu",
                        items: []
                    }]
                }]
            }];

Every item has a property items which is an array containing other items. The number of elements of the array is not specified.

I am using the knockout mapping plugin on the array to make it observableArray. And all the members of the array also become observables.

omega.franchiseInfo.observableRawData = ko.mapping.fromJS(language.rawdata);

What I want to accomplish is then unwrap the omega.franchiseInfo.observableRawData to become in its original pure javascript format. That means to be equal to omega.franchiseInfo.rawdata. I know that there are methods in knockout like ko.utils.unwrapObservable but javascript is not my strong side and I couldn't make it work for my case. Also I think that the function that will do the job should be recursive to go through all the items in the array.

Here is my fiddle:

http://jsfiddle.net/KHFn8/931/

I will be very greatfull if somebody can help me with that and provide a working code. Thank You for your time and effort.

like image 356
Mdb Avatar asked Aug 09 '12 10:08

Mdb


People also ask

What is ko ObservableArray?

An observableArray just tracks which objects it holds, and notifies listeners when objects are added or removed.

What is Ko utils?

ko. utils were built to support the internals of Knockout, so that it is able to have no strict dependencies. The utility functions generally do similar things that can do with libraries like jQuery and Underscore.

What is an observable array?

ObservableArray is an array that allows listeners to track changes when they occur.


1 Answers

JsFiddle

You can convert it back to JavaScript object like this :

ko.toJS(omega.franchiseInfo.observableRawData);
like image 60
Luffy Avatar answered Oct 23 '22 09:10

Luffy