Giter VIP home page Giter VIP logo

knockout.observabledictionary's Introduction

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

<script type='text/javascript' src="http://github.com/downloads/SteveSanderson/knockout/knockout-2.0.0.js"></script>
<script type='text/javascript' src="https://raw.github.com/jamesfoster/knockout.observableDictionary/master/ko.observableDictionary.js"></script>

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

<ul data-bind="foreach: person.items">
    <li>
        <span data-bind="key"></span>
        <span data-bind="value"></span>
    </li>
</ul>

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

<label>Name: <input data-bind="person.get('name')" /></label>

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.

<label>Company: <input data-bind="person.get('company')" /></label>

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.