使用Knockout绑定一个可观察的元素? [英] Get element an observable is bound to with Knockout?

查看:74
本文介绍了使用Knockout绑定一个可观察的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个理想的情况,但是由于我正在使用的另一个敲除绑定我处于这样一种情况:我需要获得一个可观察的元素,如果它确实绑定到任何东西。

This isn't an ideal situation, but due to another knockout binding I am using I am in a situation where I am needing to get the element an observable is bound to, if it is indeed bound to anything.

有没有办法做到这一点?

So is there a way to do this?

==更新==

我不想添加任何额外的上下文,因为它会混淆问题,但因为它可能得到一个更符合预期的答案就是这个场景。

I didn't want to add any extra context incase it confuses the question, but as it may get an answer more in line with expectations here is the scenario.

我正在使用敲除验证绑定,它使用 ko.validation.group(model)方法公开所有错误。但问题是只有给你文本错误,它没有给你任何关于模型的哪个部分给你那些错误的背景。所以我对源进行了一些小改动,现在传回与每个错误绑定的observable,因为这对于一些场景可能很有用,但是从这里我需要能够将它绑定到一个元素,这样我才能显示一些某种类型的内联验证。

I am using the knockout validation binding, which exposes all the errors using the ko.validation.group(model) method. However the problem is that only gives you the textual errors, it does not give you any context as to what part of the model gave you those errors. So I have made a small change to the source to now pass back the observable tied to each error, as this may be useful for a few scenarios, but from here I need to be able to tie this to an element so I can display some in-line validation of some kind.

Knockout Validation提供了一个非常基本的内联验证,它可以在元素之后创建一个span,你可以给它一个类,但是这对我的需求来说太基础了,因为目前我们正在使用Qtip和其他通知系统来显示验证错误,因此我需要能够有一个Dom元素和一个错误。到目前为止,我有一个observable和一个错误,但我需要将该可观察对象(可能是模型中的任何ko.observable()属性)与其给定的DOM元素绑定,如果它确实有元素绑定。

Knockout Validation provides a very basic in-line validation where it creates a span after your element and you can give it a class, but this is too basic for my needs as currently we are using Qtip and other notification systems to display validation errors, and because of this I need to be able to have a Dom element and an error. So far I have an observable and an error, but I need to tie that observable object (which could be any ko.observable() property from the model) to its given DOM element, if it does have an element binding.

因为我所拥有的只是一个对象而且我使用的是从模型驱动的验证而不是UI,所以问题实际上并不是通过自定义绑定来解决的。理想情况下,我需要能够破解将可观察对象(未知的 ko.observable())与元素结合起来。

As all I have is an object and I am using validation driven from the model not the UI, the problem is not really going to be solved via a custom binding. Ideally I need to be able to crack open the marry up the observable object (an unknown ko.observable()) to an element.

不要太具体项目,但我当前的项目抽象了事件被触发的验证(即 EventSystem.SendEvent(ValidationEvents.ValidationFailed,< element>,< error>) )然后验证系统侦听这些事件并将错误绑定到元素,无论是工具提示,咆哮样式通知,警报框等等。所以我试图找到最好的方法来保持从模型observables驱动验证时的抽象而不是ui的DOM元素(即jquery-ui)

Not to go too project specific, but my current project abstracts validation where events are fired off (i.e EventSystem.SendEvent(ValidationEvents.ValidationFailed, <element>, <error>)) then a validation system listens for these events and ties the error to the element, be it a tooltip, a growl style notification, an alert box etc. So I am trying to find the best way to keep this abstraction when driving the validation from the models observables not the ui's DOM elements (i.e jquery-ui)

==编辑2 ==

我被Knockout Validation知道observables放入自己的验证元素的元素的方式有点抛出,但是他们只是背负现有的值绑定,所以我只是想改变它根据它们添加任何验证元素的元素 isValidatable()方法,至少我可以将它绑定到一个observable的每个错误的方式,对于任何带元素绑定的observable,我可以将它们绑定到元素,如果没有,那么它们只是形成广泛的验证错误。我会尝试这个,因为它应该是(尚未测试):

I was a bit thrown by the way Knockout Validation knows the elements for observables to put in its own validation elements, however they just piggy back off the existing value binding, so I am just going to change that to add the elements for any validation elements based on their isValidatable() method, at least that way for each error I can tie it to an observable, and for any observables with element bindings I can tie them to the elements, and if there are none then it is fine they would just be form wide validation errors. I will give this a try as this should be something like (not tested yet):

if(utils.isValidatable(valueAccessor())) {
    valueAccessor().extend({hasElementalBinding: true, elementalBinding: element});
}
else { 
    valueAccessor().extend({hasElementalBinding: false});
}

registerValueBindingHandler ,我会暂时搁置这个问题,因为别人有更好的解决方案。

At around line 250 in the registerValueBindingHandler, I will leave this question open for a while longer incase someone else has a better solution.

推荐答案

这赢了不是很快,所以我肯定会缓存结果,但使用jQuery的属性选择器:

This won't be very fast, so I would definitely cache the results, but something using jQuery's attribute selectors:

$('[data-bind*="Property"]')

* = 属性包含选择器: http://api.jquery.com/attribute- contains-selector /

显然,这不会捕获任何使用手动订阅的内容.subscribe 方法,但我不知道你怎么会从函数中提取元素。

Obviously this won't catch anything that subscribed manually using the .subscribe method, but I'm not sure how you would extract element's from the functions anyway.

免责声明:虽然这个解决方案可能会有效,但是听起来对我来说是一个可怕的想法,我会改为编写自定义绑定(如评论中所述)或找到其他解决方案。

Disclaimer: while this solution will probably work, this sounds like a horrible idea to me, I would instead write a custom binding (as mentioned in the comments) or find some other solution.

这篇关于使用Knockout绑定一个可观察的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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