We're in the process of developing a new infrastructure for our MVC client to minimize the need for extensive Javascript coding, especially since most of our developers are primarily working on desktop applications.
One approach I've taken for our knockout scripts is to create an Extension method that can automatically generate all the necessary knockout elements based on the model using reflection. This has been effective for simple models without computed values.
For instance, if we have a class like this:
public class AppViewModel
{
public string FirstName {get; set;}
public string LastName {get; set;}
}
The script generated and added to the view would look like this:
function AppViewModel() {
this.firstName = ko.observable('Bob');
this.lastName = ko.observable('Smith');
}
My goal now is to find a way to also support computed values from the model. For example:
public FullName()
{
return this.FirstName + " " + this.LastName;
}
This should ideally generate something similar to:
this.fullName = ko.computed(function() {
return this.firstName() + " " + this.lastName();
}, this);
In essence, what I'm seeking is a way to generate computed values based on the model structure. Any assistance or suggestions would be greatly appreciated.
Thanks in advance!
Cheers, Steve