Reputation: 60516
I use KnockoutJs and its great but i need one thing, and i can't figure it out.
That works fine but, off course, i lose the methods defined in my knockout viewModel.
Thank you very much!
This is just a sample. Maybe there are some syntax errors, but it should show what i am trying to do.
var MyViewModel = function () {
var self = this;
self.id = ko.observable();
self.subModels = ko.observableArray();
self.doSomething = function () {
alert("Hello from " + self.id());
};
};
var MySubViewModel = function () {
var self = this;
self.id = ko.observable();
self.doSomething = function () {
alert("Hello from " + self.id());
};
};
public class MyViewModel
{
public int Id { get; set; }
public List<MySubViewModel> SubModels { get; set; }
}
public class MySubViewModel
{
public int Id { get; set; }
}
return new MyViewModel
{
Id = 1,
SubModels = new List<MySubViewModel>
{
new MySubViewModel { Id = 1 },
new MySubViewModel { Id = 2 }
}
};
Upvotes: 5
Views: 672
Reputation: 22328
If you are using the ko mapping plugin, you can map the json object to a model inside the viewmodel. For example, if you are getting a list of people, you might map the json to viewModel.people. That way you can keep your viewmodel's other properties untouched.
Upvotes: 3