How to get parent values of template binding in nested binding?
I have a knockout pattern:
<script type="text/html" id="list">
<ul data-bind="foreach: items">
<li data-bind="{text: name}"></li>
</ul>
</script>
What I use like this:
<div data-bind="
template: {name: 'list', data: itemList},
myBinding: {itemType: 'foo'}
"></div>
I have a binding handler myBinding:
ko.bindingHandlers.myBinding = {
init: function(element, valueAccessor) {
var bindingValue = valueAccessor();
alert ( bindingValue.itemType ); // alerts "foo"
// now set up a jQuery click handler
$(element).on("click", "li", listItemClickHandler);
}
};
And an event handler:
function listItemClickHandler() {
var bindingContext = ko.contextFor(this);
alert( "bindingValue.itemType ???" );
});
Is there a way to get the itemTypeparent template, as provided in the user binding, in the click handler, although knockout bindingContext?
- Without adding some fake CSS class, such as
.type-foo, to<ul>(what am I doing now). - Without saving
"foo"to an array element duringmyBinding.init(). - Without binding the event handler, use the close variable (
bindingValue). - Without using jQuery
event.dataobject. I could do this, but I would like to get it from the context of the knockout binding, if that is not possible.
+3
1
5- . , , context.$itemType = bindingValue.itemType;. : http://jsfiddle.net/rniemeyer/yeN8P/
- , itemType $parent. , , :
template: {name: 'list', data: { items: items, itemType: 'foo' } }
, , items, items itemType, $parent.itemType "foo".
+7