In my last post I wrote about getting out of the “forms” business and into the “AJAX” business, which I view as a pivotal step in the development of Web applications that are both better managed and more responsive.
The problem is that once you are liberated from the tyranny of <forms> you find that you can no longer be just a “backend developer” that surfaces web pages. You must learn the language of the frontend. I know you love your strongly typed, server side, cozy blanket, and you can certainly go work for some monolithic death corp and write the same code for the rest of your life. But if you want to develop interesting applications and stay current in the flow of software technology, get over it and start learning javascript.
One’s first steps into javascript invariably go as follows:
1) Write a bunch of crap in a <script> tag in the HTML file it’s self
2) Discover what a mess that is and how hard it is to debug and move same crap into a separate file.
At that point you think you are being responsible and call it done. A year goes by and you find that you have a lot of those files, with lots of duplicated code and/or duplicated functionality with different implementations. You consolidate that into some horrible library that contains every “helper function” in the world and include the whole file on every page. Probably minify it so when it breaks it’s a pain to figure out what’s going on and now you think you’re done.
Well you’re not. You wouldn’t write your C# application like that. In fact you’d probably go on some career-ending rant if you saw it being done (much as I am doing here with js). You must treat you’re javascript application with respect if you want it to respect you. Sure it lives for, perhaps 10 seconds, but those are pivotal seconds and that time can and will grow as you feel the joy of building an app on the client side.
So how do you treat it with respect? Well, the KEY to success is to employ a pattern, a nice pattern, and stick with it for every page. JS doesn’t have a very appealing, object oriented story out of the box, but there are a few patterns for creating objects or classes that are easier and more familiar to work with. Some are better than others. And there are several frameworks that provide implementations of these patterns and generally wrap them up with some other functionality.
As I mentioned in my previous post (not smart enough to know how to point to previous post here), I like very much the Knockout.js . However, Knockout endorses a rather repugnant pattern of dispersing your javascript between your HTML and a simple javascript object. You essentially (within Knockout’s attribute) put onclick -> fire this event in my ViewModel. Their ViewModel is just a plain old javascript object with properties and functions on it. The idea of putting a bunch of behavior inside of HTML is most unpleasant. However, I am willing to concede the need for a two-way binding attribute. The payoff is so great and the offense rather minor. But when you start putting all manner of logic in the HTML attributes, it’s my loud opinion that, you have crossed the line, Jack. Furthermore, the anemic Knockout ViewModel is not very helpful, when it comes to organizing your code. Thus, I say that KO.js has an excellent two-way binding story but you should leave it at that. Don’t let it take you down the dark road of HTML decoration.
I can see how KO would be compelled to provide other functionality so as to seem more like a “framework” than a “library”, and I can see how they would try to extend what is already working so well for them (e.g. the HTML decoration). I can see this but you can’t make me use it. Other “frameworks” Angular.js for one have taken a similar direction. Angular is very popular, but their HTML wrangling makes KO look like a minor offender. I won’t go any further into my objections. If you want to debate, hit the comments.
In my next post I will write about how I use a combination of backbone.js and ko.js to create a best-of-both-worlds cocktail.
Furthermore, I am cross posting this to my personal blog if you like it so much you want to read it twice. http://cannibalcode.blogspot.com/