I have the model defined as
public class Model
{
public string Name { get; set;}
}
and a ViewModel in JS as
var vm = {
name : ko.observable()
}
Also, I have callback which should update the viewmodel:
function callback(data)
{
ko.mapping.fromJS(data, {}, viewModel);
}
But for ko.mapping plugin property 'name' not equals 'Name'.
How to define mapping from 'Name' to 'name'?
First of all, you don't need to specify the name
property in the viewModel as the mapping plugin will add it for you.
And for the casing, alter your serialization convention to lowercase the properties.
Or, you can do what I have done and accept the uppercase in your view model. Even though it doesn't sit too well with common Javascript conventions, I have accepted it as the "properties that come from the server".
I have found it pretty handy to know which ones are created from the mapping and which ones I use for view model things. Of course, you will not see the server generated / mapped properties in your view model but they will be there.
I use this to specify and create server side view models on the server, serialize them and map them to my knockout models. This way I can concentrate only on behaviour in my knockout view models.
If you need to convert any server side object to a javascript "object", you can implement that conversion in the mapping object, like I do here