Reputation: 33
I have a viewModel, there is an observable variable but when I assign the value to the observable it will become undefined.
var ScDmfViewModel = (function () {
var selectedRequestItem = ko.observable("");
return {
SelectedItemUom: selectedItemUom,
};
})();
In another Javascript File I assign the Value 1 to it:
ScDmfViewModel.SelectedItemUom(1);
But As soon as I debug this its value shows
ScDmfViewModel.SelectedItemUom():undefined
Upvotes: 0
Views: 1836
Reputation: 33
Answer is before i defined the Code I am calling a JSON Call where it will return the Value and Reset the Observable Value. Thats Why Undeifiend
Upvotes: 1
Reputation: 1121
You can try this. Define a constructor to your ViewModel with the parameter selectedRequestItem and accessor get set for modify your parameter. Like object class.
// *** definition viewModel ***
function ScDmfViewModel() {
this.selectedRequestItem = ko.observable();
this.setSelectedItemUom = function (value) {
this.selectedRequestItem(value);
};
this.getSelectedItemUom = function() {
return this.selectedRequestItem();
};
}
// ** Create vm ** //
var vm = new ScDmfViewModel();
// set data
vm.setSelectedItemUom(1);
// get data
alert(vm.getSelectedItemUom());
http://jsfiddle.net/rniemeyer/LkqTU/
Upvotes: 0
Reputation: 46323
I think this is what you wanted:
var ScDmfViewModel = (function () {
var selectedRequestItem = ko.observable("");
return {
SelectedItemUom: selectedRequestItem
};
})();
Upvotes: 0