I'm working on a simple application using MVC4 that allows me to display each individual item in an observable array to the end user, and then provides basic CRUD functionality such that a user can add, edit or delete items.
I create my observable array in the following manner:
function Ob ligorViewModel() {
var obligorArray = ko.utils.parseJson('@jsonModel');
self.obligors = ko.mapping.fromJS(obligorArray, mapping);
this.selectedObligor = ko.observable();
this.removeObligor = function (obligor) {
self.obligors.remove(obligor);
},
this.editObligor = function (obligor) {
self.selectedProduct(obligor);
};
this.addObligor = function () {
self.selectedObligor(new ??());
},
... etc.
}
I'm trying to figure out if it's possible to instantiate a new instance of the obligor element without having to define a viewmodel for that object type. It seems to be possible to edit or delete anonyous object types using the datafor or contextFor helper methods in KO, but I haven't been able to determine if there is a manner in which you can instantiate a new anonymous type.
I'm primarily trying to avoid having to define an obligor viewmodel in JavaScript as well as in C# (this is an MVC4 application) so I can avoid issues where the C# definition is changed but the JavaScript definition is not.
Any help would be appreciated.