knockout.js

Observables

Creating an observable

JS

// data model
var person = {
    name: ko.observable('Jack'),
    age: ko.observable(29)
};

ko.applyBindings(person);

HTML

<div>   
    <p>Name: <input data-bind='value: name' /></p> 
    <p>Age: <input data-bind='value: age' /></p> 
    <h2>Hello, <span data-bind='text: name'> </span>!</h2>  
</div>

Explicit Subscription to Observables

var person = {
  name: ko.observable('John')
};

console.log(person.name());

console.log('Update name');

person.name.subscribe(function(newValue) {
  console.log("Updated value is " + newValue);
});

person.name('Jane');

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow