My team was having problem with calling jQuery UI widget, specifically Button widget, to modify the look and feel of the buttons. Our root of the problem is because we are calling jQuery UI before Knockout binding happens. Because our Knockout binding is actually creating more DOM element, jQuery UI call before binding will not apply. For design reason, our jQuery UI code can’t live after Knockout binding.
This is a typical scenario where Knockout Custom Binding comes to rescue, and obviously, one of the use case.
First, we define Knockout Custom Binding:
ko.bindingHandlers.jQueryUIButton = { init: function(element, valueAccessor) { var options = valueAccessor() || {}; $(element).button(options); } }
Then, data bind:
<button data-bind="jQueryUIButton: { icons: { primary: 'ui-icon-locked' }, text: false }"></button>
Read more on how Knockout Custom Binding works here… If you are JavaScript programmer, Knockout should be in your tool belt.