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.

Dec 25, 2012

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 so consider this an early Christmas gift... it's nice to finally give something back to the community.

Aug 1, 2012

JAXDUG Presentation on Knockout JS

I presented at JAXDUG tonight on KnockoutJS. Originally I had prepared for an hour timeslot but I got scheduled for a 20 minute Speaker Idol presentation (One night, 5 speakers, 20 minutes each). All in it went really well. It was a ton of content to fit into only 20 minutes. I ran 3 minutes over and flubbed some code, but I think I pulled it off. I'd like to present on it again; I have some ideas on how to do it better.


 I've taken an almost year sabbatical from blogging but I should be back posting regularly now. For the time being I have a lot on my mind about Knockout, MVC and Less.js. The ironic thing is that since I moved from a primarily front end position about six months ago and took a job doing primarily WCF and Web Services I've had a ton of energy in my spare time to dig into fun front end stuff, unburdened by the constraints and limitations of what may be approved for use at work.

Oct 29, 2011

Named Function Expressions in JQuery & Knockout JS

Recently I was working with JQuery & Knockout in javascript and got a long running script error so I broke out the profiler to see what was wrong. Unfortunately both JQuery and Knockout code take in a lot of anonymous functions so looking at the profiler and the stack wasn't especially useful. To make the debugger and profiler more more useful I added a name to the function expression that I suspected was causing the problem similar to this:

//Changed form this:
viewModel.FooBar = ko.dependentObservable({
  read: function(){ //Note the anonymous function expression
     return this.foo() && this.bar();
  },
  owner: viewModel
});

//To this:
viewModel.FooBar = ko.dependentObservable({
  read: function readFooBar (){ //Note the name readFooBar 
     return this.foo() && this.bar();
  },
  owner: viewModel
});

These are called named function expressions (NFE's) and they can be seen in both the profiler and the call stack. As it turns out the function that I was troubleshooting didn't have a problem after all. Later the same trick allowed me to verify the real problem was in a custom binding that I had written.

Using named function expressions hasn't been common up till now because there were issues in IE8 and lower, however these issues are easily avoided by 1)giving all named function expressions unique names withing their enclosing scope and 2)NEVER referencing the NFE name in your code (IE9 and other major browsers don't allow you to do this anyway). By following these rules and making this small investment in your code you can earn huge dividends when debugging and profiling.