I have this very simple knockoutjs script. My view model contains a property called 'modules' which is an array of strings. If I have a foreach list like this it prints a list of modules for each item:
<tbody data-bind="foreach: items">
<tr>
<td data-bind="text: modules"></td>
</tr>
</tbody>
But if I want to print the number of modules instead, by adding a computed observable:
<tbody data-bind="foreach: items">
<tr>
<td data-bind="text: numModules"></td>
</tr>
</tbody>
I get into problems. 'undefined' is not a function it says on the first line of my computed function. My js code looks like this:
function AppViewModel(data) {
var self = this;
ko.mapping.fromJS(data, {}, this);
this.numModules = ko.computed(function() {
return self.modules().length;
});
};
$.getJSON("/api/items", function(data) {
var viewModel = new AppViewModel(data);
ko.applyBindings(viewModel);
});