You could access the parent node and parse its data-bind
attribute:
JSON.parse('{' + $(element).parent().data("bind") + '}')
Be careful to put double quote (") in your binding definition, as in the following jsFiddle
Sadly I can't find a more elegant way to do it.
KO lets you access the viewmodel of a dom element using dataFor
/contextFor
, but I don't see any method to get the binding definition of a dom element.
EDIT: After further investigation, you can access the parents binding with the following:
ko.bindingProvider.instance.getBindings($(element).parent().get(0), bindingContext)
It will return an object with the bindings. For example if you declare a data-bind="style: { backgroundColor: myBackgroundColor }"
you will be able to access the observable through ko.bindingProvider.instance.getBindings($(element).parent().get(0), bindingContext).style.backgroundColor
The only problem is that you can not get the observable name within the viewmodel (or at least I don't know how, except if you compare each property with your viewmodel like idiom for comparing knockout observables)