James
James

Reputation: 1945

convert array to observable array

Is there any way to convert observable to observable array?

I am asking this because there is one function in my code which expect observable array because the function uses ko.utils.arrayForEach

I have try to google but couldnt find anything.

Update

i checked what i am passing using var test = isObservable(item). its not observable that i am passing because i get false in variable test Its normal array.

So i want to convert array to observable array

Upvotes: 1

Views: 3394

Answers (2)

PW Kad
PW Kad

Reputation: 14995

This question doesn't make a ton of sense to me, but here goes -

To convert an array to an observable array, do this -

var normalArray = ['1', '2']
normalArray = ko.observableArray(normalArray);
// normalArray is now an observableArray

Upvotes: 1

ExCode
ExCode

Reputation: 444

you can use convert normal array to observable array usingko.observableArray()

var SimpleListModel = function(items) {
    this.items = ko.observableArray(items);
    this.itemToAdd = ko.observable("");
    this.addItem = function() {
        if (this.itemToAdd() != "") {
            this.items.push(this.itemToAdd()); // Adds the item. Writing to the "items" observableArray causes any associated UI to update.
            this.itemToAdd(""); // Clears the text box, because it's bound to the "itemToAdd" observable
        }
    }.bind(this);  // Ensure that "this" is always this view model
};

ko.applyBindings(new SimpleListModel(["Alpha", "Beta", "Gamma"]));

working example http://jsfiddle.net/rniemeyer/bxfXd/

Upvotes: 0

Related Questions