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
RowControls
withnew
, and you need to attach the properties to thethis
object of theRowControl
function, rather than returning a different object.The problem returning a literal object is that
ko.pureComputed
is being called with thethis
object, and if you return a different object, rather than adding properties tothis
, then thethis
object, and the result ofnew RowControl()
(i.e. the object that has an "Approved" property) are two different objects.