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.

Jul 31, 2015

Intercepting jQuery ajax requests

Recently I needed to evaluate various grid options for a website and to keep things simple I decided to fake/intercept ajax calls to inject the data I needed. I needed something that would let me pass in a wait time and response (in my case 10000 records for a grid) as ajax data and I quickly came up with something like this:

Here is a jsFiddle of the same: Simple ajax fake for mockups

Unfortunately that didn't work for Kendo UI Grid - their code calls jQuery.ajax.extend, which lets them take a little more control of ajax and completely bypasses the above code.

I briefly considered tapping into xmlhttprequest, but a cursory look said that would be a lot of work. Then it occurred to me that jquery ajax uses deferreds under the hood and that I could just hack those instead.

Here is a jsFiddle of the same: jQuery Ajax Intercept

With a little time both examples could be modified to be more generic, but for what I was doing it wasn't really worth it (though I cleaned them up for you a little). Hope this helps.

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.