I'd like to add some "ArrayCollection" like functionality to a ko.observableArray();
i.e removeItemAt(index)//dispatches item removed event addItem(item)//dispatches item added event etc
I notice in most of the ko examples this is handled in the model. I have a fairly rich model with typed collections so would like to nest this in the collection/array itself.
Is this approach advisable with ko?
Extending
observableArrays
is easy and a perfectly reasonable thing to do.The easiest way to accomplish this is to add functions to
ko.observableArray.fn
. There is now a documentation page for this technique.This is a little bit different than what you are after, but here is a nice implementation of a dictionary in ko that may be of interest to you: https://github.com/jamesfoster/knockout.observableDictionary