KnockoutJS rocks for web development
The problem that knockoutjs solves.
( 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
Now let’s see how we can create the example with knockoutjs. Check the code using knockoutjs here
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.