从子元素访问敲除绑定 [英] Access knockout binding from child element

查看:107
本文介绍了从子元素访问敲除绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的话:

 < div data-bind =enable:false> 
< div data-bind =someCustomBinding:myValue>< / div>
< / div>

我可以从自定义绑定中访问父容器的启用绑定?



==澄清:==



在自定义绑定中,您可以使用valueAssessor访问当前的绑定值。你也可以通过所有绑定访问其他绑定,甚至通过bindingContext在不同的相关上下文中访问其他绑定。



我只是想知道你是否可以访问父HTML的绑定元素从自定义绑定(以类似于valueAccessor的方式)



这样的东西(sudo代码):

  ko.bindingHandlers.someCustomBinding = 
{
init:(element,valueAccessor,allBindings)=>
{
var parentIsEnabled =
ko.GetContextFor($(element).parent())。get('enable');
}
}


解决方案

你可以访问父节点并解析其 data-bind 属性:

  JSON.parse('{'+ $(element).parent()。data(bind)+'}')

请注意将双引号()放在绑定定义中,如以下
jsFiddle



可惜我找不到更优雅的方式



KO允许您使用 dataFor / 访问dom元素的viewmodel contextFor ,但我没有看到任何方法来获取dom元素的绑定定义。



编辑:
经过进一步调查,您可以访问绑定以下的父母:

  ko.bindingProvider.instance.getBindings ($(element).parent()。get(0),bindingContext)

它将返回一个带有绑定的对象。例如,如果您声明一个 data-bind =style:{backgroundColor:myBackgroundColor},您将可以通过 ko.bindingProvider访问observable .instance.getBindings($(element).parent()。get(0),bindingContext).style.backgroundColor



唯一的问题是否您无法在viewmodel中获取可观察的名称(或至少我不知道如何,除非您将每个属性与您的viewmodel进行比较,例如用于比较淘汰可观察量的成语


Lets say i have this:

<div data-bind="enable: false">
    <div data-bind="someCustomBinding: myValue"></div>
</div>

Is is possible for me to access the 'enable' binding of the parent container from within the custom binding?

== Clarification: ==

In a custom binding you can gain access to the current binding value with the valueAssessor. you can also gain access to other bindings through allBindings and even values on different related contexts via the bindingContext.

I was just wondering if you could access the binding of a parent html element from within a custom binding (in a similar way to the valueAccessor)

Something like this (sudo code):

ko.bindingHandlers.someCustomBinding=
{
    init: (element, valueAccessor, allBindings) =>
    {
        var parentIsEnabled = 
             ko.GetContextFor($(element).parent()).get('enable');
    }
}

解决方案

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)

这篇关于从子元素访问敲除绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆