Knockout.js is a declarative way to bind Javascript View Models to HTML views. It is definitely a very good framework for building single page applications. But, it has a few drawbacks. But before going over the drawbacks, I will briefly highlight the problem that it solves in ASP.NET MVC.

Central to the ASP.NET MVC theme is the Model class. The Model class is used to render the view with data. Since Model is specific to a View, we used to create a standard Model (similar to DB entity) and a View Model (specific to the view). With Knockout.js, there is no need to create a View Model within the ASP.NET MVC framework.

Instead, we create Javascript View Models. Knockout.js binds these javascript View Models to the View. Knockout.js alleviates the ViewModel problem for MVC developers by shifting the creation of View Models in scripts, and not in C# server-side presentation layer.

ASP.NET MVC developers can now write Views with Knockout markup, standard Models and controller actions which are AJAX / JSON based. The javascript View Models have functions which can trigger an AJAX postback, receive data in the form of JSON, and fill themselves up. Pretty neat.

But, here is the biggest problem of Knockout.js. If a web application does not have a consistent theme of representing data, then the number of View Models created can increase dramatically.

Consider a Employee model which needs to render in a custom list format and a standard tabular format. It is obvious that we have two different views for rendering the employee data. But, behind the scenes, we have to create two javascript View Models to ensure that the views get bound properly. This is an unexpected problem with Knockout.

The problem exist because javascript do not have a rich programming model. It does not lend itself well to inheritance. It is a bit absurd to create a javascript framework for representing View Models. Though such a task is quite possible, it is not within the realm of average developers. So, we are left with creating multiple View Models for representing the same kind of data in different view formats.

Creating multiple view models (with annoying amounts of similarity) is the biggest drawback of Knockout.js. For the time being, I am using Cut-Paste to create multiple View Models and I am staying away from javascript inheritance.

The drawback of Knockout.js
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *