Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending knockout observable array

Tags:

knockout.js

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?

like image 480
Chin Avatar asked Jan 26 '12 03:01

Chin


1 Answers

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

like image 154
RP Niemeyer Avatar answered Sep 24 '22 00:09

RP Niemeyer