Extending knockout observable array

2019-04-05 04:24发布

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?

标签: knockout.js
1条回答
Evening l夕情丶
2楼-- · 2019-04-05 04:57

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

查看更多
登录 后发表回答