KnockoutJS focuses on “Structured Coding” in the Front-End to make code more manageable in complex scenarios of Application. The way Knockout tackle this is by separating the Data from the View which changes dynamically as the View changes without any page reload.
We get many features like Automatic UI refresh, Dependency Tracking of the dynamic ViewModel, Bindings to bind Data from the ViewModel etc.
One of the feature that I will focus in this blog is creating Custom Bindings in Knockout.
A simple predefined Binding :
View in Fiddle : http://jsfiddle.net/rhn02o66/3/
Now lets create a Custom Binding.
There are two main properties that are enclosed within a custom Binding.
- init: This event is triggered when the ViewModel is loaded that encapsulates the custom binding.
- update: This event is triggered whenever a knockout observable to which binding is attached within the ViewModel is updated or modified.
A simple custom Binding that inherits the functionality of predefined Binding used in above snippet:
View in Fiddle : http://jsfiddle.net/rhn02o66/6/
If you look at the HTML code you would see a new binding called limitInput (to limit the characters to 6) instead of regular textInput binding. Well, this is our custom binding which requires a function to understand the input and a way to initialize itself.
The event init takes 4 parameters –
- element stores the reference from which the binding is called.
- valueAccessor has the value stored in parameter of the binding context, is an observable.
- allBindingsAccessor reference to other bindings defined in ViewModel.
- bindingContext is the functional context of the ViewModel from which it is evoked.
(For further Reference on passed parameters see http://knockoutjs.com/documentation/custom-bindings.html )
Further inside init event, the textInput binding is called which makes allowedInput to inherit all the objects and properties.
This allowedInput function is needed because we want to bind limitInput field so that it automatically inherits all the properties available to ‘textInput’ binding.
In update event, we have put a check on the length of the input-text due to which the length can not increase more than 6 character and in case it is greater than 6 character then this function will trim the extra characters and pass it to the valueAccessor().
In this way, we can create our custom binding according to our needs by also using the functioning of the predefined bindings.
Now let us look at some more examples in which the custom bindings can be used:
Custom binding for numeric input only:
View in Fiddle : http://jsfiddle.net/rhn02o66/7/
In the above, limitInput is replaced by new custom binding called numericInput which takes numbers only as input and discard any other input as defined in its updateevent. Rest of the functioning is similar to what we described in our limitInput example.
Now, lets create a more generic binding using the above examples which will have the functionality of both custom bindings, limitInput, numericInput.
Generic binding for restricted input:
View in Fiddle : http://jsfiddle.net/rhn02o66/8/
Continuing with what we learnt in the first two examples, the above example explains the usage of the Regular Expressions for limiting the input from the user.
Custom binding for email validation:
Regular Expression is a very powerful tool for string checking. One common way in which we can use Regular Expression is for Email validation. Combining it with the power of Custom Bindings we can create a custom binding for Email validations.
Lets dive into more technical details.
View in Fiddle : http://jsfiddle.net/rhn02o66/9/
If the input email is invalid then the background of the input element will turn to red.
Custom bindings are pretty generic and can be applied to various use cases.