Dependency Tracking – KnockoutJS

Dependency Tracking

Using a single object called dependency tracker i.e. ko.dependencyDetection you can determine when the value get updated. When you declare a computed observable, KO immediately gets its initial value and updated computed observable.

Lets see an example for Dependency Tracking:

<!DOCTYPE html>
<title>Dependency Tracking - KnockoutJS</title>
<script src="" type="text/javascript"></script>
<form data-bind="submit: addCountry">
<b>Add Country:</b>
<input data-bind='value: newCountry, valueUpdate: "afterkeydown"'/>
<button type="submit" data-bind="enable: newCountry().length > 0">Add Country</button>
<p><b>Your Country List:</b></p>
<select multiple="multiple" width="50" height="auto" data-bind="options: countries"> </select>
var AddCountry = function(countries) {
this.countries = ko.observableArray(countries);
this.newCountry = ko.observable("");
this.addCountry = function() {
if (this.newCountry() != "") {

ko.applyBindings(new AddCountry(["India", "Australia", "South Africa", "Zimbabwe"]));

Leave a Reply

Your email address will not be published. Required fields are marked *