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

Functions turned undefined by mapping #31

Closed Ixonal closed 11 years ago

Ixonal commented 11 years ago

If an object to be mapped contains a function, that property will be set to undefined (the property still exists, it is just undefined). This should probably function like the knockout mapping plugin, where these functions are simply passed through

coderenaissance commented 11 years ago

This was intentional as the mapping was only intended to be used on json objects returned from an ajax call. By definition these objects would now have any functions on them. How are you using knockout viewmodel that makes this functionality useful?

Ixonal commented 11 years ago

It was possibly not the best idea, but my first shot at using knockout, had a very large view model to deal with that was several levels deep in various places, so I created classes on the client side to mirror those on the server side (though the only thing in the constructor was $.extend(this, options)) and added any additional functionality I wanted through that type's prototype. Initially I just took the json response, chunked it into the proper type, and then ran the mapping plugin on it. With your mapping plugin, I could make it work, but had to reverse the order (map the response, then feed it to the type).

Either way, I'm not really seeing a downside to passing the functions through. You'd just be assuming you're getting a random javascript object as opposed to a raw deserialized json object.

coderenaissance commented 11 years ago

By limiting the project to just JSON objects I was able to simplify processing in a number of ways. If I add in functions I would be taking a step down a slippery slope of processing regular javascript objects. What if someone wanted to be able to add observables to their model before running through processing? An argument could be made for that and many other things. But each one I add would make the code larger, slower, and more complex. I'm trying to limit the code to be very good and very fast at only one thing: converting between json objects and knockout viewmodels.

Thanks for the feedback.

Ixonal commented 11 years ago

Closing this a lil too fast there, man. Simple solution to your point and mine would be to simply pass anything you don't immediately recognize through. You must already recognize this contingency because those items are already set to undefined (as opposed to not being added).

For future reference, it's a little insulting to just up and close the issue while someone is actively engaging in the conversation. It's kind of like saying "Talk to the hand".