knockout-collection
A knockout observable array as hashmap
usage
var item1 = id: ko label: 'Item 1'; var item2 = id: ko label: 'Item 2' var collection = item1 key: 'id' ; tobeundefined; collection; tohave; collection;collection; tohavelength0;
api
If you need direct access to the underlying ko.observableArray
you can use collection.items
. Use this only to bind, not to modify.
construction
You can pass an array as items.
'my' 'array' 'items';
If you want to manage an ko.observableArray
you can pass it as items
and set the option: reference
:
var items = ko;var collection = items key: 'id' reference: true ; collection; // items() will be [item2]
.add(item)
adds an item to the collection which is identified by the value of the (observable-)property with name options.key
.
The item is only added, if it isn't already contained in the collection
.remove(item)
removes the item from the collection. If it isnt available in the collection nothing is done
.get(keyValue)
returns the item with the value of the (observable-)property with name options.key
equal to keyValue
.
it will return undefined
if the collection does not contain the item.
.contains(item)
checks if the item is contained in the collection.
.length
You can use the length property as you would use it for an array:
var collection = 'i1' 'i2'; console; // is true
.removeAll()
Removes all items from the collection