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
83
votes
1 answer

knockout.js - Get ViewModel from DOM element

is is possible to get the binded ViewModel JavaScript object from a given DOM element? ko.applyBindings( gLoginViewModel, document.getElementById("login-form") ); ko.applyBindings( gLoginViewModel, document.getElementById("register-form") ); and…
Dirk Boer
  • 8,522
  • 13
  • 63
  • 111
83
votes
5 answers

Force a computed property function to run

Given a computed property vm.checkedValueCount = ko.computed(function(){ var observables = getCurrentValues(); //an array of ko.observable[] return _.filter(observables, function(v) { return v() }).length; }); suppose getCurrentValues() can…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
81
votes
3 answers

Access $parent's $parent in knockout view - nesting context

Updated for brevity How can I reference the $parents' $parent in nested Knockout foreach / with bindings? Example - // <-- Doesn't work …
PW Kad
  • 14,953
  • 7
  • 49
  • 82
80
votes
4 answers

Setting the id attribute with knockoutjs including a prefix

I'm using KnockoutJS to iterate over an object, like this: Now this all works. But the problem i have is that it sets the id of the button to just a number. So it looks like this:
w00
  • 26,172
  • 30
  • 101
  • 147
79
votes
3 answers

Prevent firing the blur event if any one of its children receives focus

I have a div on a page that shows some info about a particular category (Image, Name etc). When I click on the edit image it puts the category into edit mode which allows me to update the name. As you can see from the image below it shows that…
Thwaitesy
  • 1,503
  • 2
  • 13
  • 15
78
votes
13 answers

Knockout.js v2.3.0 error "You cannot apply bindings multiple times to the same element"

I've just upgraded to Knockout.js 2.3.0 and now I'm getting this error: You cannot apply bindings multiple times to the same element. which I wasn't getting in 2.2.1. I'm getting a partial view from my MVC controller and adding it to the page after…
AlignedDev
  • 8,102
  • 9
  • 56
  • 91
77
votes
3 answers

How to add/insert an item into an ObservableArray at a certain position with Knockout.js

All the knockout examples I have found seem to add a new item to the end of an ObservableArray using something like: viewModel.SomeItems.push(someNewItem); This of course places the item at the end of the array. How to I add an item to the…
Mark Robinson
  • 13,128
  • 13
  • 63
  • 81
76
votes
4 answers

What is the best way to hide the screen while knockout js bindings are being built?

I'm a huge knockoutjs fan. I use it for all my web development now and simply love it. One thing that I've not been able to figure out though is how to hide the UI while the knockoutjs bindings are being built. For example, I have a very robust user…
Luc
  • 1,830
  • 2
  • 23
  • 38
76
votes
10 answers

JSON parsing error syntax error unexpected end of input

I got the following piece of code function pushJsonData(productName) { $.ajax({ url: "/knockout/SaveProduct", type: "POST", contentType: "application/json", dataType: "json", data: " { \"Name\" : \"AA\" }…
Shawn
  • 5,130
  • 13
  • 66
  • 109
76
votes
6 answers

Subscribe to observable array for new or removed entry only

So yes I can subscribe to an observable array: vm.myArray = ko.observableArray(); vm.myArray.subscribe(function(newVal){...}); The problem is the newVal passed to the function is the entire array. Is there anyway I can get only the delta part? Say…
Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
73
votes
3 answers

Angular.js and ASP.NET MVC 4

I have an ASP.NET MVC 4 project and I'm stuck on an architectural decision on which JavaScript framework or library to use Angular.js or Knock.js. I am currently leaning towards using Angular.js over Knockout.js, but don't want to find out midway…
72
votes
2 answers

Bind to simple array of strings

If I want to bind a template to a plain old array of strings, what do I put in the ${??} expression? I hope this snippet isn't too short so as to confuse:
where authors…
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
72
votes
1 answer

Knockout template using data-bind to image src property not working

I cannot see what is wrong here but the image does not display using the following Knockout template: