I am trying to solve a small problem with ko mapping. The scenario is that my ViewModel is basically a collection of objects. Each of the objects is created from a json call, in this way:
var ViewModel = ko.observableArray();
$.getJSON(url, function(data) {
ViewModel.push(ko.mapping.fromJSON(data));
});
This works perfectly and I can do all sort of magic in my HTML. The question is if for example I want to add something to my collection, let's say to support client-side "Add and Edit" scenario. I would like to do something like:
<input type="button" value="add new" data-bind="click: AddNew" />
And I would like the AddNew function in the ViewModel to be something like:
function AddNew() {
this.push(// WHAT HERE?);
}
Basically I need to push an object which is identical to the other already existing, but of course with all blanked out properties...
I was thinking of a way of "cloning" an object form the list and setting all the observables to empty but I would not know where to start I'm afraid :/