Questions tagged [knockout-2.0]

Knockout.js is an open source JavaScript library for dynamic HTML UIs using the Model-View-View Model (MVVM) pattern. This tag is for questions specific to the 2.0 releases.

Knockout.js is an open source JavaScript implementation of the Model-View-ViewModel (MVVM) UI pattern. This tag is for version 2.0 specific questions. See also the main tag for general information about Knockout.js.

Version 2.0 of Knockout.js as announced by its creator included the following new features:

  1. Control flow bindings such as if, ifnot, with, and foreach;
  2. Containerless control flow, e.g. <!-- ko foreach: products -->;
  3. Access to parent binding contexts through contextual variables such as $parent;
  4. Cleaner event handling using the click binding;
  5. Binding providers (and hence external bindings)
  6. Throttling by extending observables.

Find the v2.0.0 source on GitHub.

1007 questions
150
votes
1 answer

Angular.js vs Knockout.js vs Backbone.js

I am considering to use either Knockout or Angular or Backbone for my personal project. I need to build some bigger, longer-running client-side interactions to go with my server-side stuff. I want a simple and effective way to manage data-driven…
Beaver21
  • 1,666
  • 2
  • 10
  • 8
86
votes
5 answers

Get previous value of an observable in subscribe of same observable

Is it possible in knockout to get the current value of an observable within a subscription to that observable, before it receives the new value? Example: this.myObservable = ko.observable(); this.myObservable.subscribe(function(newValue){ //I'd…
KodeKreachor
  • 8,672
  • 10
  • 44
  • 64
84
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,411
  • 7
  • 33
  • 44
83
votes
9 answers

How to get Selected Text from select2 when using

I am using the select2 control, loading data via ajax. This requires the use of the tag. Now, I want to retrieve the selected text. (The value property in the data-bind expression sotres the id only) I have tried…
robasta
  • 4,491
  • 5
  • 32
  • 53
77
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
  • 23,892
  • 26
  • 94
  • 145
75
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
  • 13,399
  • 23
  • 77
  • 119
55
votes
12 answers

Is there a reason I would use Knockout MVC instead of Knockout JS?

Another user suggested Knockout MVC to handle some AJAX posting issues. I read a little on it and I see it is a wrapper around Knockout JS. So I wonder what are the real differences between the two? Should I bother with Knockout JS since Knockout…
dotnetN00b
  • 4,745
  • 11
  • 58
  • 90
55
votes
1 answer

Knockout: computed observable vs function

When using knockout, what is the advantage of using read-only computed observables rather than simple functions? Take the following viewmodel constructor and html snippet, for example: ​ var ViewModel = function(){ var self = this; …
Duncan
  • 1,133
  • 2
  • 11
  • 20
54
votes
2 answers

$index+1 in Knockout foreach binding

I need to display $index+1 in a table. If I just use the $index all the elements will start from 0, I need to start at 1. Here's the documentation of knockout: http://knockoutjs.com/documentation/foreach-binding.html In there you can find this…
Sanchitos
  • 7,552
  • 4
  • 44
  • 49
45
votes
2 answers

Replace all elements in Knockout.js observableArray

I have an observableArray in my view model. After creating the vm I wish to completely replace the data the observableArray. Here's how I'm doing it: //Initial Setup var vm = {}; vm.roles = ko.observableArray([]); ko.applyBindings(vm);…
C.J.
  • 6,411
  • 7
  • 33
  • 44
44
votes
4 answers

How to Clear Contents of an observableArray That was Populated from Previous Visits to a View

I have a Single Page Application that uses knockout for the data binding. The CAApproval.html view in my single page application has an observeablearray named AllCertificates in the viewmodel code. It populates fine on the page. When you navigate…
Chris
  • 755
  • 2
  • 11
  • 26
43
votes
1 answer

Get dynamically inserted HTML to work with knockoutjs

I'm using JQuery DataTables for all my tables because of all the nice built-in features, but it seems the only way to customize the table layout is to set the "sDom" option attribute for the DataTable and use something like…
T.Ho
  • 1,190
  • 3
  • 11
  • 16
35
votes
3 answers

Knockout.js "if Binding" on multiple booleans

Is it possible to use Knockout's if binding on more than one boolean? Such as
... I've tried a lot of different syntax, but can't seem to find the right syntax. I'm not sure…
Brad Bamford
  • 3,263
  • 2
  • 20
  • 27
35
votes
3 answers

Knockout checkbox change event sends old value

I'm having a problem with knockout "checked" binding. It seems that "change" event at checkbox return old value, before it is updated(so if it was unchecked it will return false). I don't think that I can subscribe to the value since I have it…
akhabaiev
  • 439
  • 1
  • 4
  • 10
35
votes
2 answers

alternate row style with $index binding

I am having trouble getting an alternate-row css class applied to a knockout template with a foreach binding context. I am using knockout 2.1 with the available $index context variable. This is whats confusing: My Template
  • Leland Richardson
    • 2,675
    • 2
    • 18
    • 26
  • 1
    2 3
    67 68