KnockoutJS rocks for web development
I’m working on my second project using KnockoutJS and it has saved me a lot of time with the benefit of keeping my clientside code organized. I have enjoyed frontend development since i learned javascript and jquery and knockoutjs makes it more fun to build Web applications. The reason is that i can focus on the client logic and forget about dealing with dom elements updates.
The problem that knockoutjs solves.
I love JavaScript and find JQuery very useful , however i don’t like plumbing code specially when dealing with updating DOM elements from model properties or visceversa. Even trivial applications can benefit from a databinding mechanism like the one knockoutjs offers.
( trivial example ) suppose you need to create a web application that captures two fields FIELD1 and FIELD2 using input elements and display the sum of both elements whenever one of them changes.
Normally you would have the following steps:
- Create a js method called recalculateSum that executes the business logic (adding 2 numbers)
- Capture the change event of FIELD1 and FIELD2 and recalculate the result
- Display the result in some DOM Element
check the demo (without knockoutjs) here The html
The javascript (using only jquery)
Now let’s see how we can create the example with knockoutjs. Check the code using knockoutjs here
Note that with knockout we don’t have to reference the html elements in our javascript code. We also don’t have to update manually the text of the span element that displays the result. This is possible because we created a computed value and let knockoutjs handle the update of the element whenever field1 or field2 change.
The binding is really easy, we use the attribute data-bind
Knockout uses the concept of observables so it can track changes in the values and update the corresponding html element, it also handle the common change events in form elements and update the observable value.