knockout.js - Access knockout binding from child element -
lets have this:
<div data-bind="enable: false"> <div data-bind="somecustombinding: myvalue"></div> </div> is possible me access 'enable' binding of parent container within custom binding?
== clarification: ==
in custom binding can gain access current binding value valueassessor. can gain access other bindings through allbindings , values on different related contexts via bindingcontext.
i wondering if access binding of parent html element within custom binding (in similar way valueaccessor)
something (sudo code):
ko.bindinghandlers.somecustombinding= { init: (element, valueaccessor, allbindings) => { var parentisenabled = ko.getcontextfor($(element).parent()).get('enable'); } }
you access parent node , parse data-bind attribute:
json.parse('{' + $(element).parent().data("bind") + '}') be careful put double quote (") in binding definition, in following jsfiddle
sadly can't find more elegant way it.
ko lets access viewmodel of dom element using datafor/contextfor, don't see method binding definition of dom element.
edit: after further investigation, can access parents binding following:
ko.bindingprovider.instance.getbindings($(element).parent().get(0), bindingcontext) it return object bindings. example if declare data-bind="style: { backgroundcolor: mybackgroundcolor }" able access observable through ko.bindingprovider.instance.getbindings($(element).parent().get(0), bindingcontext).style.backgroundcolor
the problem can not observable name within viewmodel (or @ least don't know how, except if compare each property viewmodel idiom comparing knockout observables)
Comments
Post a Comment