coderenaissance / knockout.viewmodel

The knockout viewmodel plugin is the fastest, smallest, cleanest, most flexible way to create a knockout viewmodel.
http://coderenaissance.github.com/knockout.viewmodel
106 stars 28 forks source link

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

Open SteveRyherd opened 11 years ago

SteveRyherd commented 11 years ago

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?

coderenaissance commented 11 years ago

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.