new state(102, "Florida", 1),
new state(201, "Bavaria", 2),
new state(202, "Berlin", 2),
new state(302, "Karnataka", 3),
new state(303, "Kerala", 3)
]),
selectedState: ko.observable(),// Nothing selected by default
cities: ko.observableArray([
new city(1011, "Fremont", 101),
new city(1026, "Tallahassee", 102),
new city(1032, "Atlanta", 103),
new city(1043, "Raleigh", 104),
new city(1051, "Houston", 105)
]),
selectedCity: ko.observable() // Nothing selected by default
};
viewModel.selectedStates = ko.dependentObservable(function () {
var country = this.selectedCountry(), countryId;
if (country) {
countryId = country.countryId();
return ko.utils.arrayFilter(this.states(), function (state) {
return state.countryId() === countryId;
});
}
return [];
}, viewModel);
viewModel.selectedCities = ko.dependentObservable(function () {
var state = this.selectedState(), stateId;
if (state) {
stateId = state.stateId();
return ko.utils.arrayFilter(this.cities(), function (city) {
return city.stateId() === stateId;
});
}
return [];
}, viewModel);
ko.applyBindings(viewModel);
</script>
----------------------------------------------------------------------------------