The earlier answer is correct, but often I pass in functions to custom bindings (a function that checks permissions, or determines what to do based on something else, etc). What I really needed was to unwrap any function, even if it's not an observable.
The following recursively unwraps EVERYTHING:
ko.utils.unwrapFunction = function (func) {
if (typeof func != 'function') {
return func;
}
else {
return ko.utils.unwrapFunction(func());
}
};
Here is an example of a simple custom binding I wrote:
//replaces single and double 'smart' quotes users commonly paste in from word into textareas and textboxes with normal text equivalents
//USAGE:
//data-bind="replaceWordChars:true
//also works with valueUpdate:'keyup' if you want"
ko.bindingHandlers.replaceWordChars = {
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var bindingValue = ko.utils.unwrapFunction(valueAccessor);
if (bindingValue) {
$(element).val(removeMSWordChars(allBindingsAccessor().value())); //update DOM - not sure why I should need to do this, but just updating viewModel doesn't always update DOM correctly for me
allBindingsAccessor().value($(element).val()); //update viewModel
}
}
}
This way bindingValue always contains a value. I don't need to worry about if I passed in a function, an observable, a value, or a even a function inside an observable. This will properly unwrap everything until it gets at the object I want.
Hope that helps someone.