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

knockout.mapping toJS does not seem to work in nodejs

Open grofit opened this issue 11 years ago • 4 comments
trafficstars

I was just testing out some stuff in nodejs and if I were to do:

var ko = require("knockout");
ko.mapping = require("knockout.mapping");

var somePojoWithObservables = new blah();
// populate above

var output = ko.mapping.toJS(somePojoWithObservables);
console.log(output);

It is outputting all the functions etc rather than just the raw object without observables. I notice that this project does not seem to have a package.json and other forks have added this, however it has Steve Sanderson's name on it so I thought it would be best to put down in here.

I have tried just using it without adding to the ko model i.e:

var ko = require("knockout");
var mapping = require("knockout.mapping");

and then using the mapping variable for the mapping jobs, but that gives same result. I am using one of the 11 versions of node, but everything else is working fine (validation and the custom ko.mapping.merge library).

grofit avatar Jun 28 '14 07:06 grofit

@grofit When it comes to plugins this is getting tricky. To get the dependency chain right peerDependencies must be used instead of asking the developer to install knockout separately. From you code example I guess that's something either old or due to aforementioned problem.

crissdev avatar Feb 23 '15 16:02 crissdev

This is actually a double knockout issue, it was mentioned and diagnosed elsewhere but there was no simple fix: https://github.com/knockout/knockout/issues/1624

grofit avatar Feb 23 '15 16:02 grofit

Thanks for the details. If I were to agree on using 2 instances of KO in (i.e.) nodejs then ko.hasPrototype would need to change to use a string based type check. That would fix this issue. But I still believe that plugins should use peerDependencies, as it's the case for Grunt, for instance.

crissdev avatar Feb 23 '15 16:02 crissdev

Yeah I use peerDependencies to sidestep the issue in my scenario

grofit avatar Feb 23 '15 16:02 grofit