Knockout + Bootstrap 3 Radio Buttons

The bootstrap buttons and the knockout checked binding are still not playing nice: knockout uses the click event inside the checked binding to tigger the underlaying observable to change bootstrap subscribes on the click event to do the toggling but calls e.preventDefault() so KO won’t be notified about the click. One possible solution is to … Read more

knockoutjs: can we create a dependentObservable function with a parameter?

In Knockout, bindings are implemented internally using dependentObservables, so you can actually use a plain function in place of a dependentObservable in your bindings. The binding will run your function inside of a dependentObservable, so any observables that have their value accessed will create a dependency (your binding will fire again when it changes). Here … Read more

Prevent event bubbling when using the checked binding in knockoutjs

It sounds like you were on the right track. You basically want to do the equivalent of: click: function() { return true; }, clickBubble: false OR This could be done in a custom binding like: ko.bindingHandlers.stopBubble = { init: function(element) { ko.utils.registerEventHandler(element, “click”, function(event) { event.cancelBubble = true; if (event.stopPropagation) { event.stopPropagation(); } }); } … Read more

make an input only-numeric type on knockout

Is better to create custom binding http://knockoutjs.com/documentation/custom-bindings.html which accept only allowed characters [0-9,.] as numeric representation. put this line into your view <input id=”text” type=”text” data-bind=”numeric, value: number”> put this line into your model (remember to bind number as observable property) ko.bindingHandlers.numeric = { init: function (element, valueAccessor) { $(element).on(“keydown”, function (event) { // Allow: … Read more

success callback after knockout.js finishes rendering all the elements

You have the afterRender callback in knockout.js: foreach: { data: myItems, afterRender: renderedHandler } Here’s documentation. Inside your handler check whether the length of the rendered collection is equal to the length of the items collection. If not don’t execute the full rendered logic that you intend to use. renderedHandler: function (elements, data) { if … Read more

Unique ids in knockout.js templates

An alternative that does not rely on the order that the fields are bound is to have the binding set an id property on the data itself, which would need to be an observable. ko.bindingHandlers.uniqueId = { init: function(element, valueAccessor) { var value = valueAccessor(); value.id = value.id || ko.bindingHandlers.uniqueId.prefix + (++ko.bindingHandlers.uniqueId.counter); element.id = value.id; … Read more