I have created the following model and I don't understand why the pureComputed observable ApprovalIconCSS cannot access the Approved() observable from its function context.
function KOViewModel() {
var self = this;
self.IsAdding = ko.observable(false);
self.IsEnabled = ko.observable(true);
self.ApprovalType = ko.observable(0);
var RowControl = function() {
return {
Approved: ko.observable(null),
ApproverNotes: ko.observable(''),
ApprovalIconCSS: ko.pureComputed(function() {
if (this.Approved() == 0)
return 'glyphicon glyphicon-remove-circle';
if (this.Approved() == 1)
return 'glyphicon glyphicon-ok-circle';
if (this.Approved() == 2)
return 'glyphicon glyphicon-time';
return '';
}, this)
};
};
self.RowControls = ko.observableArray([RowControl()]);
}
Appreciate if someone can shed light why the context isn't accessible. Cheers!
You need to call
RowControlswithnew, and you need to attach the properties to thethisobject of theRowControlfunction, rather than returning a different object.The problem returning a literal object is that
ko.pureComputedis being called with thethisobject, and if you return a different object, rather than adding properties tothis, then thethisobject, and the result ofnew RowControl()(i.e. the object that has an "Approved" property) are two different objects.