1

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

1
  • Also as a note ko.utils.arrayForEach doesn't require an observable array. You could also just say $.each instead if you wanted to Commented Apr 26, 2014 at 14:11

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

i get error "The argument passed when initializing an observable array must be an array, or null, or undefined."
Are you sure it is an array? Sounds like its not.
When i see in debug mode what i pass is of type object anonymus function
I think i get json object with this part "Methods,EntityAspect,prototype".
0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.