Questions tagged [knockout.js]

Knockout.js is an open source JavaScript library for dynamic HTML UIs using the Model-View-View Model (MVVM) pattern.

Knockout.js is an open source JavaScript library implementation of the Model-View-View Model (MVVM) UI pattern in JavaScript and HTML.

Knockout.js is a pure JavaScript library with no external dependencies. It is supported in all major browsers, IE 6+, Firefox 2+, Chrome, Opera, Safari (desktop/mobile).

Knockout.js works with any web framework.

The key features of Knockout.js include:

  • Free, open source (MIT license)
  • Small & lightweight — 55kb minified
  • Declarative Bindings: Easily associate DOM elements with model data using a concise, readable syntax.
  • Two-way binding: When your view model's state changes, your UI updates automatically. When you change form element values, the view model's state updates automatically.
  • Dependency Tracking: Implicitly set up chains of relationships between model data, to transform and combine it.
  • Templating: Quickly generate sophisticated, nested UIs as a function of your model data.
  • Components - 3.2.0+: Controls or Widgets made up of self-contained reusable chunks of code.

Helpful links

Stack Snippet Starter Pack

HTML:

<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-debug.js"></script>

<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2>

JavaScript:

var ViewModel = function(first, last) {
    this.firstName = ko.observable(first);
    this.lastName = ko.observable(last);
 
    this.fullName = ko.pureComputed(function() {
        return this.firstName() + " " + this.lastName();
    }, this);
};
 
ko.applyBindings(new ViewModel("Planet", "Earth")); 
20270 questions
115
votes
6 answers

twitter bootstrap autocomplete dropdown / combobox with Knockoutjs

I have a requirement where I HAVE TO use bootstrap autocomplete dropdown, BUT user can have free form text in that dropdown if they wish. Before you think about TypeAhead, I could use Bootstrap TypeAhead textbox, but I need to have the dropdown…
103
votes
4 answers

How to conditionally push an item in an observable array?

I would like to push a new item onto an observableArray, but only if the item is not already present. Is there any "find" function or recommended pattern for achieving this in KnockoutJS? I've noticed that the remove function on an observableArray…
jaffa
  • 26,770
  • 50
  • 178
  • 289
103
votes
5 answers

How to have multiple data-bind attributes on one element?

I need to have multiple data bindings on one element. For example, I want a href as well as a html data-binding on one a tag. I have tried this,
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
101
votes
4 answers

Getting "Cannot read property 'nodeType' of null" when calling ko.applyBindings

I have this knockout code: function Task(data) { this.title = ko.observable(data.title); this.isDone = ko.observable(data.isDone); } function TaskListViewModel() { // Data var self = this; self.tasks = ko.observableArray([]); …
user745235
100
votes
4 answers

Determine if an object property is ko.observable

I'm using KnockoutJS version 2.0.0 If I'm looping through all properties of an object, how can I test whether each property is a ko.observable? Here's what I've tried so far: var vm = { prop: ko.observable(''), arr:…
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
98
votes
11 answers

Change event on select with knockout binding, how can I know if it is a real change?

I am building a permissions UI, I have a list of permissions with a select list next to each permission. The permissions are represented by an observable array of objects which are bound to a select list:
guy schaller
  • 4,710
  • 4
  • 32
  • 54
95
votes
4 answers

How to template If-Else structures in data-bound views?

I constantly find myself using this idiom in KO-based HTML templates:
some mark up here
user1255162
  • 1,258
  • 2
  • 9
  • 13
86
votes
8 answers

Binding true / false to radio buttons in Knockout JS

In my view model I have a IsMale value that has the value true or false. In my UI I wish to bind it to the following radio buttons:
C.J.
  • 6,789
  • 7
  • 36
  • 45