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

observable and computed are not working properly Knockout

I have the following knockout code that implements the folowing role: field1 + field2 -field3 = field4 $(function () { ko.applyBindings(new AppViewModel()); }); function AppViewModel() { this.original = ko.observable(0); this.improvements =…
user3378165
  • 6,546
  • 17
  • 62
  • 101
4
votes
1 answer

Knockout js binding base64 image to image tag

i am trying to bind image to HTML image tag using knockout js,its not working,i have base64 string in database and i am fetching it from database but its not working for me below is my code //viewModel// function ViewModel(data) { self = this;…
kitty sarvaj
  • 517
  • 5
  • 10
  • 24
4
votes
1 answer

Convert to Knockout, then convert to Angular, bad idea, or go straight to Angular

Have an MVC app with razor pages. Converting it to use Knockout. Then once complete, will go back and convert all Knockout to Angular. I want to get educated on that strategy. Not a pros and cons question; I mean logically it is inefficient as…
4
votes
1 answer

Multiple "pattern" validation extenders using knockout

If I extend a knockout observable like so var x = ko.observable(). extend({ pattern : { params: someRegex, message: "An error" } }) .extend({ pattern : { params: someMoreRegex, message: "Another…
Sim
  • 570
  • 1
  • 10
  • 22
4
votes
1 answer

Is numeric animation behaviour possible with knockout observable?

For example you have simple model like this: var ViewModel = function() { var timer, self = this; this.value = ko.observable(10); this.value2 = ko.observable(10); this.value.subscribe(function(newValue){ …
Nick Bondarenko
  • 6,211
  • 4
  • 35
  • 56
4
votes
0 answers

Javascript in Edge only works with devtools open

The problem I have is that in MS edge, my imagemap functionality's only work partially. It is powered by the knockout FW. On bigger imagemaps, it doesn't registers the entire imagemap into the viewmodel. As a result all the hover effects and the…
4
votes
1 answer

Cant get array to show in line chart using either JS/KnockoutJS

I have a ajax call to the database to get date fields, which are basically like this.["2016-3", "2016-5", "2016-6", "2016-7", "2016-8"], When I call them from the db I get them exactly as the way you see in the array, but I cannot get them working…
Masnad Nihit
  • 1,986
  • 2
  • 21
  • 40
4
votes
1 answer

Can I get a list of elements currently bound by knockout?

Consider the following example: ko.applyBindings(viewModel, document.getElementById('id')); Is there some way via using knockout to get a list of "applied bindings?" I ask because I have a situation where I am getting the dreaded: Uncaught Error:…
TheWebs
  • 12,470
  • 30
  • 107
  • 211
4
votes
1 answer

How to manually add Compiled Knockout Template in DOM?

I am using Salvattore jQuery Masonry to display items in grid. The items are rendered using Knockout Template before Salvattore is initialized. For example:
Bhaskar Dabhi
  • 841
  • 1
  • 11
  • 28
4
votes
2 answers

Angular2 version of knockout's computed observable

I am currently working on porting some code that I have written from Knockout to Angular2. I really like the construct of computed observables in knockout which means the results of that function are only computed when the obeservables it depends…
Gabe O'Leary
  • 2,010
  • 4
  • 24
  • 41
4
votes
2 answers

object HTMLInputElement instead of value in knockout js textInput binding

I want to pre fill fields of the form on load with existing data from database. On page load i am making an ajax call which queries the data and assign the returned data to a knockout observable array. client.js: function clientModel() { …
Saurabh
  • 664
  • 2
  • 12
  • 30
4
votes
2 answers

Is there anyway to inspect knockout observables using Chrome debugger?

I am trying to inspect observables using the Chrome debugger. I am using the knockout plugin, but it isn't showing information on the observables I'm interested in. The workaround I've been using is to set the observable to the global window object…
Musical Shore
  • 1,361
  • 3
  • 20
  • 41
4
votes
2 answers

Knockout JS not clearing components

So here's a weird KnockoutJS problem I've never actually come across before. I'm working on an application that uses Knockout Components very heavily. In one part of the app, I have an editor page that's built dynamically from a JSON driven backend,…
shawty
  • 5,729
  • 2
  • 37
  • 71
4
votes
1 answer

How to bind property on parent object with knockoutjs?

I'm developing a application with knockout.js framework. I have one viewmodel like that: var MyViewModel= { Id: ko.observable(), CountryCode: ko.observable(), NormalizedAddress: { COUNTRY_CODE: ko.computed(function ()…
ilMattion
  • 1,841
  • 2
  • 24
  • 47
4
votes
2 answers

How to return an item from an observable array by property

I have this code: var id = event.target.getAttribute('id'); var matchedItem = ko.utils.arrayForEach(self.ProductEffectImagesToMatch(), function(item) { if (item.index == id) { return item; } } ); I want to get the item by index…
Laziale
  • 7,965
  • 46
  • 146
  • 262
1 2 3
99
100