0

I am trying to replace the contents of an observable array with an array that is returned from a promise.

I initialize an observable array like so

let contents = ko.observableArray([]);

And the array that is returned from the promise looks something like this

[{name : test, code : 0, country : UK}]

How can replace the empty observable array with the contents of the array that is returned from the promise?

I have tried this

promise.then(array => {contents(array)});

As well as this

promise.then(function(value){contents(value)})

but neither of them add anything into the observable array.

1
  • 1
    I can't duplicate your problem with a simple example. Commented Jan 16, 2019 at 0:31

1 Answer 1

0

Check this link

you should first delete its content (if any) through observableArray method called removeAll.

// empty the array
contents.removeAll()

// insert the new array values
promise.then(array => {ko.utils.arrayPushAll(contents, array)});

// or
promise.then(function(value){ko.utils.arrayPushAll(contents, value)});
Sign up to request clarification or add additional context in comments.

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.