Observables to refresh the UI automatically
The last example shows us how Knockout binds data and the user interface, but it doesn't show the magic of the automatic UI refresh. To perform this task, Knockout uses observables.
Observables are the main concept of Knockout. These are special JavaScript objects that can notify subscribers about changes, and can automatically detect dependencies. For compatibility, ko.observable
objects are actually functions.
To read an observable's current value, just call the observable with no parameters. In this example, product.price()
will return the price of the product, and product.name()
will return the name of the product.
var product = Product(1,"T-Shirt", 10.00, 20); product.price();//returns 10.00 product.name();//returns ...
Get KnockoutJS Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.