knockout.viewmodel
knockout.viewmodel copied to clipboard
Mapping to existing model
Hi there, is there a way to replace ko.mapping.fromJS(data, {}, this); from original mapping plugin with one function from knockout.viewmodel?
I'd like to map the data to my original model, the original plugin is pretty handy in this case but lacks some other useful stuff from your plugin.
Thanks in advance.
Yeah, I miss this feature as well.
+1
This is a must have. When you're using class constructors with default data, the mapping overrides the class object. I mean: var MyClass = function(data) { var self = this; if (data) { self = ko.viewmodel.fromModel(data,MyMapper); }; };
var instanceOfMyClass = new MyClass({someproperty: true});
instanceOfMyClass class will be MyClass but won't have the someproperty. If I return self at the end of the constructor, the class will have the someproperty, but the class of instanceOfMyClass will be Object.
I would contribute something like this: fromModel: function fnFromModel(model, options, result) { var settings = getPathSettingsDictionary(options); initInternals(this.options, "Mapping From Model"); var tempResult = recrusiveFrom(model, settings, rootContext); if (!result) return tempResult; for(var k in tempResult) result[k]=tempResult[k]; },
True, this is a must have and is a glaring omission. I don't want to wipe out my entire viewmodel with every ajax request!
I want to see this feature as well!
I tried to use smth like this:
parsedModel = ko.viewmodel.fromModel(existingData, options);
ko.viewmodel.updateFromModel(initialNode, parsedModel);
But it does not work at all. I noticed one more detail. updateFromModel requires both initialNode and parsedModel to be Object. It makes no sense to me! I want to work with my custom model...