Struggling with defining a function inside my viewmodel.
I retrieve json data using jquery getJSON and then map it to the viewmodel.
$.getJSON('/Company/GetCompanies', function(data) {
var viewModel = new CompanyViewModel()
viewModel.model = ko.mapping.fromJS(data)
ko.applyBindings(viewModel)
});
Here is the viewmodel. The goal is to extract one of the properties of the viewmodel using a function called companyName
var CompanyViewModel = function() {
var self = this;
self.companyName = function() {
return model.CompanyName;
};
}
Then I aim to utilize this function like
<span data-bind="text: companyName" />
However, the JavaScript function doesn't seem to be evaluated correctly and ends up being displayed as text.
Reviewed numerous Knockout examples online, but they all rely on computed observables.
Is there a way to achieve this? Much appreciated.