knockout.viewmodel
knockout.viewmodel copied to clipboard
Possible to create an "Add New" method using this plugin and then select the result?
I see "pushFromModel" which appears to create a new model correctly but it doesn't return a model. Using the extend method, I don't see any way to select this newly created model -- Am I missing an existing method for this?
I think this might be what you are trying to do:
var model = {
myItems:[
{key:"test", value:5}
]
};
var options = {
extend:{
"{root}.myItems[i]": function(user){//extend each item
user.isDeleted = ko.observable(false);
}
}
};
var viewmodel = ko.viewmodel.fromModel(model, options);
viewmodel.myItems.pushFromModel({key:"test2", value:7});//item recieves "{root}.myItems[i]" extend
var lastPushed = viewmodel.myItems.slice(viewmodel.myItems().length - 1)[0];
For the moment this should do what you need (awkward I know). It seems reasonable that pushFromModel should return the item post mapping. I'll consider this for a future release.