One of the major features we’re working on for the next release of Kendo UI is a fresh JavaScript implementation of the Model View ViewModel (MVVM) pattern. For those not familiar, MVVM is a popular model of development that relies heavily on declarative bindings and observable elements and collections, buffeted by a ViewModel, or an object representation of view-specific data. The goal is to reduce the amount of "View code" developers must write. MVVM was originally conceived for Microsoft's WPF and Silverlight platforms, but it has found fans among JavaScript developers, too.
In keeping with Kendo UI’s mission to deliver everything you need for HTML and JavaScript development, we've decided to provide an MVVM implementation out-of-the-box.
In the process of developing our MVVM implementation, we have conducted extensive research and testing on how to build a JavaScript MVVM implementation that is fast and easy to use. In the process, we made some interesting observations that we'll share over the coming weeks. Ultimately we think our implementation is going to raise the bar for JavaScript MVVM performance, especially on lower-powered mobile devices.
Still, we know we're not the first to tackle MVVM for JavaScript, so we want to spend this post addressing the one of the other major JavaScript binding libraries, KnockoutJS.
We like KnockoutJS. We think the work that Steve Sanderson (and countless contributors) have done with this popular open source library is nothing short of impressive, and we know a TON of people love Knockout. In fact, Kendo UI MVVM will feel similar in some ways to Knockout because our research verified that certain KO choices are the best for performance (such as single attribute binding statements). In short, Steve and company have done a good job.
That’s why we’ve done more than any other JavaScript UI library to support Knockout integration out-of-the-box. And we’re not done. We want to do more.
Through conscious effort, we’ve built Kendo UI widgets to automatically work with much of KnockoutJS’s functionality. Specifically, here’s how you can use Knockout with Kendo UI today. You can:
We provide this automatic integration by triggering the DOM “change” event of the underlying Kendo UI HTML element. This makes Knockout aware that something has changed in the UI.
Here's a simple example that binds a Kendo UI DatePicker to a Knockout observable:
(If you have any trouble running the embedded jsFiddle, try clicking the "+" button to run it outside of the embed frame.)
Kendo UI's support for Knockout is not 100%. To fully support Knockout’s observable model requires some extra code in Kendo UI that doesn’t exist today. As a result, here’s what doesn’t work today:
In some ways, these problems are solvable with workarounds.
We’ll address the first problem listed here by providing a “Knockout aware” data source. When you bind Kendo UI widgets to this data source, it will provide the “observable hooks” necessary to make KO changes update the Kendo UI widgets.
In fact you can already do this with the Kendo UI AutoComplete (and only the AutoComplete) today to get a sense of what this approach looks like.
The second problem listed above can be solved now with a simple line of code:
$("#myElement").bind("change", function() { $(this).data("kendoDropDownList").select(this.selectedIndex); });
This will bind the "change" event of the underlying DOM element (which gets triggered by Knockout) to code that will “force” a Kendo UI widget to update and refresh the UI. With this code added, Kendo UI will "listen" for changes Knockout triggers in the HTML and the update the rich Kendo UI widget.
There are a few other places where support is missing today, too, such as the Slider, where we have not yet "taught" the widgets to trigger the "change" event for KO. This support will be shored-up in our next release.
Even though we have our own MVVM implementation arriving in March, we remain committed to making Kendo UI easy to use with any library you love. Kendo UI is everything you need with "escape hatches" to use your own libraries when you want them.
So we want to ask you, how do you want us to improve our Knockout support?
We have two choices to continue improving our support:
There are pros and cons to each approach.
PROS
CONS
PROS
CONS
So here’s your chance to help. If you’re a Knockout fan and you want to use KO with Kendo UI, what approach would you prefer? We want to make Kendo UI the best rich UI library for use with KO, but there are some hurdles to clear to make that integration seamless.
We’re ultimately very excited to deliver a great MVVM implementation while simultaneously trying to provide premiere support for popular libraries like KnockoutJS. Let us know how you’d like to see Kendo UI evolve and take an active role in shaping Kendo UI in to the ultimate toolset you want.
Todd Anglin is Vice President of Product at Progress. Todd is responsible for leading the teams at Progress focused on NativeScript, a modern cross-platform solution for building native mobile apps with JavaScript. Todd is an author and frequent speaker on web and mobile app development. Follow Todd @toddanglin for his latest writings and industry insights.