SteveSanderson / knockout.mapping

Object mapping plugin for KnockoutJS
Other
546 stars 767 forks source link

knockout.mapping toJS does not seem to work in nodejs #195

Open grofit opened 10 years ago

grofit commented 10 years ago

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

crissdev commented 9 years ago

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

grofit commented 9 years ago

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

crissdev commented 9 years ago

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.

grofit commented 9 years ago

Yeah I use peerDependencies to sidestep the issue in my scenario