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

Mapping to existing model

Open betrayedjinn opened this issue 11 years ago • 5 comments
trafficstars

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.

betrayedjinn avatar Jan 25 '14 04:01 betrayedjinn

Yeah, I miss this feature as well.

kirkov avatar Feb 07 '14 12:02 kirkov

+1

unsafecode avatar May 12 '14 11:05 unsafecode

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]; },

rolandrybka-circeo avatar Jul 22 '15 09:07 rolandrybka-circeo

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!

JoeWarwick avatar Sep 17 '15 05:09 JoeWarwick

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...

Helen1987 avatar Jul 05 '16 09:07 Helen1987