Usual observables allow us to create some property for which Knockout.js detects any changes. However, what if we want to work with a collection of objects and detect add and remove operations? The Knockout.js solution is observable arrays. We can use this special kind of arrays with the help of ko.observableArray
. In this section, we will discuss how to use it in general, how to add new elements in an observable array, how to remove elements, and some useful manipulation functions for observable arrays.
Very often, the real ViewModel objects contain collections of some elements. In this section, we will consider a very simple example with a collection in the ViewModel to demonstrate observable array usage. Let's add the children
array for person
, as shown in the following code (PersonalPage-Arrays1.html
).
The Model will be as follows:
var person = { children: ["Jonnie", "Jane", "Richard", "Mary"] };
The ViewModel will be as follows:
var personViewModel...