Search code examples
design-patternsknockout.jsviewmodelrevealing-module-pattern

Knockout.js: View-Model function returns code?


Thanks to the great Tutorial from John Papa. I tried my best to implement the Revealing Module Pattern for my ViewModel using knockout.js

It is a very simple example:

  • There are 2 Customer objects.
  • Add the ID from both Objects and return it.

http://jsfiddle.net/ThomasDeutsch/EHYfT/

Somehow I get not the value returned - but the code that is executed ? I expected to see the result "3" on the screen :)

What am I doing wrong?


Solution

  • You are adding two observables together. Observables are functions. Make sure you use the () form to get the actual value, like this:

    addId = ko.computed(function () {
        return customer1.Id() + customer2.Id();
    }),