Skip to main content

Why use KnockoutJS


I put together a bit of a proposal for my team and thought I'd post it today.

THE GOAL
As far as I know, we’re all in favor of decoupling.

Decoupling code has the purpose of separating code into pieces so that changes are easy.  MVC is an example, in that its purpose is to separate the logic layer (controller) from the view, and pull the objects out into their own little place (the model) so they can pass between the two.

We do this with N-Tier applications as well, as we’ve started pulling our model and logic out into separate projects in visual studio, creating a logic dll and a model dll that is used by the web application (or other application) front end, and separating the database from the webservice from the front end.

THE PROBLEM
In decoupling, some people get a bit too gung ho and try to decouple everywhere.   In fact, you’ll see this on Stack Overflow a lot.  The problem I see is that we’re decoupling at the wrong place much of the time.  If you think of decoupling like making independent pieces of a robot that work uniquely, you would want the robot’s arm to attach to any of a number of robots and work the same regardless.

The difficulty here is that in my robot example, the arm isn’t generic, it’s proprietary.  Here’s a bit of sample code I’m toying with.

Let’s say we decide to switch our back end from C# to Perl or PHP or node.js, or we simply want to reuse this code in another project.  What then?  We can’t reuse this code.  It’s essentially trash.  That’s not what decoupling is about.  We’re supposed to have code that’s decoupled so that it is reusable.

Good decoupling separates the pieces in generic ways that they can be changed smoothly.  We’ve all had this issue when buying a laptop or phone charger and finding out that the makers didn’t use generic hardware.  Instead, we have to buy some proprietary hardware or a new phone/laptop just to get by.

THE SOLUTION
Fortunately, there is a solution.  What if the code I’m showing you instead looked like this:

Look at that, pure HTML5 markup, no mixed in C# (razor’s nice, don’t get me wrong).  The only tweak is that little data-bind attribute.  That’s from the library I’m suggesting that will clean this code up.

KnockoutJS works a lot like some other JS libraries to make JS more object oriented, but it has the most important thing of all.  There are a lot of high-quality tutorials on both the project’s primary website http://learn.knockoutjs.com/ as well as Pluralsight Training.

It can use HTML templates cleanly, meaning it can wrapPartialViews without the need for C#, if we really wanted to go to that extreme.  But the truth is it does it by binding to a Javascript model.  Does this mean we have to re-create our model in JS? Nope.  Knockout can take a JSON string from the server parse it and use it as our ViewModel on the fly.  Basically, on the front end we’re working with the same objects we were on the back end, without any proprietary syntax.

I really think this is the best way to cleanly decouple our View.

Comments

  1. Well, that's certainly _a_ way to do the decoupling, but having just worked on an extensive Backbone.js project, I'd recommend a quick shootout between several different frameworks to see which one fits your needs and taste best.

    After all, there's Backbone.js, Ember.js (which at least on paper I like better), AngularJS, KnockoutJS, etc., etc. You can see a slew of different choices over on http://todomvc.com/

    ReplyDelete
    Replies
    1. Yup. I am loving all the JS projects that are out right now. I picked Knockout over the others purely because of its tutorials which are better than pretty much any tutorials I've seen in years.

      Delete

Post a Comment

Popular posts from this blog

How to identify a skilled programmer during an interview

How does one identify a skilled programmer?  No company that has interviewed me could tell the difference between myself and other programmers they'd interview.  The interview process is truly a game of luck in this industry--on both sides.  Both the programmer and the company are basing their actions entirely on luck.

Companies have come up with numerous methods to attempt to discern a good programmer from a bad one.  The best tricks they have include a series of math problems, algorithms, problem solving technique tests, and even obscure programming questions, some without definitive answers.  As an example: Is there an authoritative source of information on the core principles that define object oriented programming?  I've heard everywhere from 3 to 7.  In a field of research about a synthetic concept, an authoritative answer is almost impossible to obtain.

Programmers were then forced to study to the interview.  Careercup is one of my favorite sites for this.  This almost …

When Is Software Done?

I have some very exciting news.  A piece of software I've been working on for over 2 years is released to the general public!  This is a little exciting if it were software I'd been working on for some big company.  It's very exciting because it's software I have been working on for my company.  That's right!  My company is ready to start selling software and start making money!

I'm not gonna use this blog post to talk about my company and what it does.  You can read about that in our press release.  Instead, I'm going to talk about the software industry and the concept of done.  Because, as with everything, it's more complicated than it seems.

Software is never really done
Actually that's a misnomer.  Software can really be done.  But done is sort of a quantum state--there and not there at the same time.  First and foremost, anyone can understand that software that works is complete.  If the software's purpose is to process a credit card, if th…

Managing Programmers

Working with other programmers is tricky.  That said, it's nothing compared to the job of managing programmers.  One of my favorite quotes about Perl is that (paraphrased) "a Perl developer is like a rockstar.  Now imaging having a bunch of rockstars in one room together and you will understand why you don't want an entire team of Perl developers."  It's not about Perl here though. What's important to understand is that any developer worth his salt is going to be like a rockstar.  And yes, there are a lot of professional developers out there who aren't worth their salt, but that's for another post another day.  Rockstar may not be the right term here, but think of it this way.  These guys are smart.  They may not be geniuses, but there's going to be things that they know that you don't and probably never will.

I've seen it more than once and it's not going to make some Product Managers happy, but I'm going to state a fact, an eleph…