On the quest to provide a rich user interface experience on his current project, Sensei has been experimenting with KnockoutJS by Steve Sanderson. If you haven’t reviewed it’s capabilities yet it would be well worth your while. Not only has Steve put together a great series of tutorials, but he has been dog fooding it with Knockout. The entire documentation and tutorial set is completed used Knockout. Another fine source is Knockmeout.net by Ryan Niemeyer. Ryan is extremely active on StackOverflow answering questions regarding Knockout, and also has a fine blog that offers very important insight on developing with this framework.

KnockoutJS is a great way to re-organize your client side code. The goal of this post is not to teach you KnocoutJS; rather, Sensei wants to point out other benefits – and a few pitfalls – to adopting its use. In years past, it’s been difficult to avoid writing spaghetti code in Javascript. Knockout forces you to adopt a new pattern of thought for organizing your UI implementation. The result is a more maintainable code base. In the past you may have written code similar to what Sensei use to write. Take for example assigning a click event to a button or href in order to remove a record from a table:

The proposition offered by Knockout is much different. Many others much more conversant in design patterns and development than Sensei can offer better technical reasons why you sound use Knockout. Sensei likes the fact that it makes thinking about your code much simpler. As in:

Yep, you have code mixed in with your mark up, but so what. You can hunt down what’s going on, switch to your external js file to review what deleteRecord is supposed to do. It’s as simple as that. Speaking of js files, Knockout forces you to have a more disciplined approach organizing your javascript. Here is what the supporting javascript could look like:

These Aren’t the Voids You’re Looking For

So we’re all touchy feely because we have organization to our Javascript and that’s a good thing. Here’s some distressing news – while Knockout is a great framework, getting the hang of it can be really hard. Part of the reason is Javascript itself. Because it’s a scripting language, you end up with strange scenarios where you have a property that appear to have the same name but different values. You see, one of the first rules of using Knockout is that observables ARE METHODS. You have to access them with (), as in customer.name(), and not customer.name. In other words, in order for you to assign values to an observable you must:

What? Actually, as you probably have surmised, you get .name() and .name, and this causes great confusion when you are debugging your application in Firebug. Imagine you can see that customer.name has a value when you hit a breakpoint, but its not what you’re looking for. Sensei developed a tactic to help verify that he’s not insane, and it works simply. When in doubt, go the console in Firebug and access your observable via the ViewModel; so in our case you could issue:

vm.customer.name();

When name() doesn’t match your expectation you’ve most likely added a property with a typo. Check with

vm.customer.name;

It sounds silly, but you can easily spend a half hour insisting that you’re doing the right thing, but you really confusing a property with a method. Furthermore, observable arrays can also be a source of frustration:

// This is not the length of the observable array. It will always be zero!!!
vm.customers.length == 0;
// You get the length with this syntax
vm.customers().length;

Knock ’em inta tamarra, Rocky

Had Sensei known the two tips before starting he would have save a lot of time. There are many others, and they are best described by Ryan Niemeyer in his post 10 things to know about Knockout from day one. Read this post slowly. It will save you a lot of headache. You may familiar with jQuery and Javascript, but Knockout introduces subtle differences that will catch you off guard. That’s not a bad thing, it’s just different than what you may be used to. Ryan also makes great use of JS Fiddle and answers most of his StackOverflow questions by using examples. Those examples are in many cases easier to learn from than the tutorial since the scope is narrower than the instruction that Steve Sanderson gives. It really allows you play along as you learn.