I need to figure out which element was removed from my Knockout observableArray. Please see my jsFiddle.
I can subscribe to the change, but it only returns value, which is the current array after the add or remove.
self.selectedDataPointOptions.subscribe(function(value) {
// how can I see which one was added or removed?
alert(value);
});
Michael Best's solution (subscribeArrayChanged) works very well for me too. But I need to use it from typescript and for this reason I wrote a little define source (d.ts), in a different source from original 'knockout.d.ts' for using it in a comfortable way in typescript source code.
custom knockoutext.d.ts file:
Little sample code snippet:
The proposed solution is cool, and works, but it involves cloning the array every time there is a change, then doing a compare, which is probably O(n^2).
Here is another solution: It means including another js file... but if you want some better performance, this will provide it:
https://github.com/bobwold/betterObservableArray
This replacement for observableArray (which is basically just a clone of observable array, with some extra code) uses the knockout subscription framework, and adds "add" and "remove" subscriptions.
Sample Usage:
...
...
Knockout includes
ko.utils.compareArrays
which you can use to compare one array to another. Here's a helper function that notifies for each added or removed item in the array:Here it is in action: http://jsfiddle.net/mbest/Jq3ru/
Beginning with Knockout 3.0, you can use the
arrayChange
event to do this more easily. More info is here: http://blog.stevensanderson.com/2013/10/08/knockout-3-0-release-candidate-available/