I am combining an Options binding and a Computed, and am getting the error in the Firebug console:
TypeError: this.selectedCountryNeverNull is undefined
This is the relevant part of the ViewModel:
// Constructor for an object with two properties
var Country = function (name, population) {
this.countryName = name;
this.countryPopulation = population;
};
var viewModel = {
availableCountries: ko.observableArray([
new Country("UK", 65000000),
new Country("USA", 320000000),
new Country("Sweden", 29000000)
]),
selectedCountry: ko.observable(), // Nothing selected by default
selectedCountryNeverNull: ko.observable(), // has default
selectedCountryDesc: ko.computed(function () { return '*' + this.selectedCountryNeverNull.countryName + '*'; }, this)
};
and this is the select:
<select data-bind="options: availableCountries,
optionsText: 'countryName',
value: selectedCountryNeverNull"></select>
I left out the optionsCaption:
so that the first array element is the default, and the value is never null.
Firebug says the error is in the ko.computed
line, and I tried to add ()
parentheses here and there, but with no avail.
I want to do much bigger ko.computed
stuff, but isolated my problem in this extension of an example from the knockout site.
Thanks for any help in trying to understand the parentheses issues in general and mine particular.