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

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

Example of knockoutjs pattern for multi-view applications [closed]

There are a few directions that you could go with this one. One option is to call ko.applyBindings with distinct view models against separate DOM elements like: var viewModelA = { name: “Bob” }; var viewModelB = { price: 50 }; ko.applyBindings(viewModelA, document.getElementById(“aContainer”)); ko.applyBindings(viewModelB, document.getElementById(“bContainer”)); http://jsfiddle.net/9abgxn8k/ In this case, you would want to make sure … Read more

Can cleanNode() be used to clean binding?

ko.cleanNode is used internally by Knockout to clean up data/computeds that it created related to the element. It does not remove any event handlers added by bindings or necessarily understand if a binding made changes to the DOM. This can definitely cause problems like having multiple handlers attached to an element when it is subsequently … Read more