I am using knockout.js library. I have a Boolean value and I am saving it as Observable. But when I try to get the value in a drop down selected index change event. It gives me this error :
Object function viewModel(model) {
this.stocks = ko.observableArray(model.stocks); this.isGeneral = ko.observable(model.generalStockEnabled); } has no method 'isGeneral'
Here is my code for registering my model :
function viewModel(model) { this.stocks = ko.observableArray(model.stocks); this.isGeneral = ko.observable(model.generalStockEnabled); } here is my drop down list event :
$('#enableGeneratInventorydl').change(function () { if ($('#enableGeneratInventorydl :selected').val() === "True") { alert(viewModel.isGeneral()) } else { } }); I am calling this at page initialize
ko.applyBindings(new viewModel(data)); the data object has a boolean value generalStockEnabled.
Any Idea ?
viewModelis just the constructor and does not refer to the object created bynew viewModel(data). You should assign created object to a variable first, and then use it to call its members:var vm = new vieeModel(data); alert(vm.isGeneral());