Code Renaissance is about building great teams and great software. By exploring best practices, team interactions, design, testing and related skills Code Renaissance strives to help you create the team and codebase that you've always wanted.

Releasing knockout.viewmodel plugin 1.0

If you've used knockout.js then you are familiar with the knockout.mapping plugin for creating viewmodels. As awesome as knockout is, the mapping plugin is what made it really practical to use. Unfortunately as I've tried to knockout in business applications I've found the mapping plugin had a few problems:

  • It was slow on large view models, particularly in older browsers(IE7/8) and on large arrays.
  • Creating viewmodels wasn't a one step process... after calling mapping you'd then have to extend the viewmodel further. 
  • It provided no easy way to organize the viewmodel creation code which becomes a problem on larger viewmodels.
  • It didn't allow much customization in how the viewmodel was created and what customization there was was confusing and not intuitive to use.

I've been working the past couple of months to address these issues by creating a new mapping plugin. The knockout.viewmodel plugin uses recursive algorithms to create viewmodels over 85% faster than the original mapping plugin and was designed to allow  viewmodel creation, extension, and management to take place entirely through the plugin. This provides a natural organization to the code. 

Documentation and downloads for the javascript plugin can be found here and it is currently hosted on GitHub. It can also be found on Nuget and can be installed through visual studio by searching Nuget for "knockout.viewmodel".

I have already created an extensive set of unit tests for the project. These include unit test that were run against the current version of knockout.mapping for basic mapping. This means that without mapping options specified knockout.viewmodel should generate identical viewmodels to knockout.mapping.

In case you find any problems with the plugin I have created a jsfiddle unit test that you can modify, save and submit as part of a new issue ticket. I plan to add unit tests to the test suite for all issues. I really could not have taken this as far as I have without unit tests and they will definitely be a permanent part of the project. 

Mad props to QUnit Team for their phenomenal javascript unit testing framework, to the GitHub Team for their recently released GitHub for Windows, and to Steve Sanderson for creating knockout and knockout.mapping, which has revolutionized the way I create user interfaces for the web.

This is my first venture into open source so I'm sure I have many mistakes ahead of me. I appreciate the patience of the community and any help you can provide. Please let me know what you think.

And in closing... Merry Christmas everyone! We just crossed into Christmas day here, I got my pup bathed by a Houston mobile dog groomer, and everyone is happy. So consider this an early Christmas gift... it's nice to finally give something back to the community.

5 - What do you think?:

Patrick Lafrance said...

Hi,

Nice mapping plugin! I really like the way your processing options work. I feel like it give me much more control than ko.mapping. The "unmap" feature is AWESOME, Ive been looking for that for a while.

One question thought, for the "custom" processing options is it possible to specify a function handler for ALL the properties of the model that will be map to an observable ? Something like

options:{
custom:{
"{all}": function ...
}
}

Thank you !

Patrick Lafrance said...

Hi again,

By the way, the reason why I would like to do something like this is to extend ALL the created observables with a specific knockout observable extender.

Thank you !

DH said...

Sorry the closest thing that you can do is define a shared function and reference it for every property that you want it applied to:

var options = {
shared:{
"myExtend":function(item){
//extendItems
return item;
}
},
extend:{
"item.prop1":"myExtend",
"item.prop2":"myExtend",
}
};

Patrick Lafrance said...

Ok !

Thank you.

Anonymous said...

This looks promising. However, the name "Knockout Viewmodel" plugin is the worst name I have ever witnessed.

The reason is it will be NEARLY IMPOSSIBLE to google search this plugin and not get mixed results that are simply discussing knockout and viewmodels.

Please.... please... consider changing the name to something that doesn't conflict with preexisting vernacular in this domain.

How about Knockout Maptastic? :)