I have a data-bind that has a lot of logic in it. Currently I have to paste it several places to get what I want. I would like to be able to make it a function that is only in one place preferably as a method of the KO object. The data-bind I need to move is the one that creates the italicized count text.
data-bind="text: ($data.children)?($data.children.filter(function(e){return e.enabled()}).length + ' Children On'):('No Children')"
You can create a function on your main viewmodel which returns a
ko.computedwith the calculation:Then you can use like this inside in your template:
Demo JSFiddle.