knockout.viewmodel icon indicating copy to clipboard operation
knockout.viewmodel copied to clipboard

Possible to create an "Add New" method using this plugin and then select the result?

Open SteveRyherd opened this issue 12 years ago • 1 comments

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?

SteveRyherd avatar Aug 20 '13 00:08 SteveRyherd

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.

coderenaissance avatar Aug 21 '13 03:08 coderenaissance