I'm having some trouble accessing model objects from my view model. This is likely just a JavaScript/KnockoutJS familiarity issue, so any help is appreciated. Here's my code:
<!-- VIEW -->
<select data-bind="options: allTypes,
optionsCaption: 'Choose...',
value: chosenType"></select>
<div data-bind="text: chosenType"></div>
<div data-bind="text: chosenValues"></div> <!-- << This line not working -->
<script type="text/javascript">
/*-- VIEW MODEL --*/
function ViewModel() {
this.chosenType=ko.observable();
this.chosenValues=allValues[this.chosenType]; // <-- Problem here?
}
/*-- MODEL --*/
var allTypes=["Animals","Plants","Minerals"];
var allValues={
"Animals":["Pig","Big pig","Owl"],
"Plants":["Aloe","Fern"],
"Minerals":["Bauxite","Chromite"]
};
ko.applyBindings(new ViewModel());
</script>
I think the problem is likely in how this.chosenValues is being declared. Thanks for reading.
ko.observable()?ko.observablebecausethis.chosenTypedoesn't need a default value, and I think that part's working OK.new ViewModel()? IsviewModel.chosenValuesreturning undefined?