Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jamesfoster/knockout.observableDictionary

a comprehensive implementation of an observable dictionary in KnockoutJS
https://github.com/jamesfoster/knockout.observableDictionary

Last synced: 2 months ago
JSON representation

a comprehensive implementation of an observable dictionary in KnockoutJS

Awesome Lists containing this project

README

        

To use the knockout observable dictionary you simply need to reference knockout and then this


Then pass a javascript object to ko.observableDictionary() as follows.

var person = {
name: 'Joe Bloggs',
height: 180,
'hair colour': 'brown'
};

var viewModel = {
person: new ko.observableDictionary(person)
};

ko.applyBindings(viewModel);

To loop over the items in the dictionary simply data bind to the items property. Each item has a `key` property and a `value` property






You can also data bind to specific elements within the dictionary using the method `get`

Name:

You can even data bind to elements which don't exist yet. In this case, if you update the value it will add a new item to the dictionary.

Company:

To set a value on the dictionary in code use either:

* the `set` method: `viewModel.person.set('hair colour', 'blue');`. or
* the `get` method: `viewModel.person.get('hair colour')('blue');`.

The obersvableArray methods `indexOf`, `remove`, `sort` and `push` have also been overridden to behave as expected with dictionaries e.g. `viewModel.person.remove('height')` and `viewModel.person.indexOf('hair colour')`

Enjoy