Notes

KnockoutJS

Edit on GitHub

Web Development
2 minutes

the viewmodel properties are just plain JavaScript strings, they have no way of notifying anybody that they’ve changed, so the UI stays static. That’s why Knockout has a concept of observables - these are properties that automatically will issue notifications whenever their value changes.

Observables

They are just properties that advertise to the UI when they are changed

  • to read/write an observable’s value, you call it as a function

Simplest KnockoutJS example

 1<body>
 2  <!-- Bind an HTML element -->
 3  <h1 data-bind="text: animal">Monkey</h1>
 4
 5  <!-- Import Knockout -->
 6  <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.1/knockout-latest.js"></script>
 7
 8  <script>
 9    // Create a ViewModel
10    var viewModel = {
11      animal: ko.observable(),
12    }
13
14    viewModel.animal('Elephant')
15
16    // Activate KnockoutJS so that it updates values for `data-bind`
17    ko.applyBindings(viewModel)
18  </script>
19</body>

Notes

  • Without Typescript, there are no errors. If you have a data-bind that doesn’t exist, it’ll fail silently (took me an hour to figure out that one). If you’re binding a ViewModel that doesn’t exist, it’ll fail silently. Typescript is pretty much a necessity if you want to keep your sanity and save your time..

  • make sure you have the browser console open, that’s one place that’ll show you some errors.

  • var self = this saving a reference/alias to this as self is a common convention in ES5 (and Knockout). it is useful when you have functions inside function and they have their own this and you need to reference back to self (i.e. the parent function’s this..)

  • applyBindings() takes an instance of the ViewModel and a Selector (node inside the DOM tree). Selector is useful when you have multiple ViewModels and want to restrict the activation to a certain area..

  • ko.computed() let’s you take values from other observables and create a consolidated result

1ko.computed(() => {
2  // return `${self.firstName} ${self.lastName}` // [object Object] [object Object]
3  return `${self.firstName()} ${self.lastName()}` // Aamnah Awesome
4})
  • Observables are special JavaScript objects that can notify subscribers about changes and can automatically detect dependencies. Think of observables as pieces of state that can change

  • Observables are actually functions in KnockoutJS because not all browsers support JavaScript getters and setters