简体   繁体   English

基于textInput的自定义敲除绑定

[英]Custom knockout binding based on textInput

I have quite a specific requirement for a custom binding that I'm not sure how to implement. 我对自定义绑定有一个非常具体的要求,但我不确定该如何实现。

I want to use the new textInput binding but unfortunately it doesn't play well with a placeholder polyfill I use. 我想使用新的textInput绑定,但是不幸的是,它在我使用的占位符polyfill上效果不佳。 I have been using the value binding with valueUpdate on keyup but I like the many edge cases that textInput can also handle. 我一直在keyup上使用valueUpdate与value绑定,但是我喜欢textInput也可以处理的许多边缘情况。

My requirement here is that in instances where the value equals the value of the placeholder attribute the viewmodel should have an empty string. 我的要求是,在值等于占位符属性的值的情况下,视图模型应具有一个空字符串。

Here's a JSBin comparing the current and textInput based methods, open it in a browser like IE8 to see the differences. 这是一个JSBin,用于比较当前方法和基于textInput的方法,在类似IE8的浏览器中将其打开以查看不同之处。

Edit 编辑

Attempting to re-explain the issue. 试图重新解释该问题。 The problem comes from knockout binding the value of an input field to the viewmodel and the polyfill for an input working by replacing empty input fields with a value equal to the placeholder attribute (pretty much the approach taken by all polyfills for placeholder). 问题来自于通过将空的输入字段替换为等于占位符属性的值(将所有polyfill用作占位符的方法)而将输入字段的值绑定到视图模型和polyfill上的问题。

Previously I've been using the value binding and using valueUpdate on the keyUp event. 以前,我一直在使用值绑定和在keyUp事件上使用valueUpdate。 This means that the bound viewmodel property updates as someone types. 这意味着绑定的viewmodel属性会随着有人键入而更新。 As the polyfill replaces the value in JS on the blur event the input field will have a value of the placeholder attribute but the viewModel value will still be an empty string. 当polyfill在模糊事件中替换JS中的值时,输入字段将具有占位符属性的值,但是viewModel值仍将为空字符串。

The textInput binding is more complex but more useful, however the polyfill replacing an empty input value also triggers the knockout update. textInput绑定更复杂,但更有用,但是polyfill替换为空的输入值也会触发敲除更新。 Due to the complexities and edge cases that textInput handles reusing the old approach of valueUpdate on keyUp is not an option. 由于textInput处理的复杂性和极端情况,因此无法在keyUp上重用valueUpdate的旧方法。 What I want instead is to create a binding like textInput but is aware of such polyfills. 我想要的是创建一个类似于textInput的绑定,但是知道这种polyfills。

Basically If I have 基本上如果我有

<input data-bind="placeholderTextInput:query" type="text" placeholder="Search..."/>

viewModel = {
    query: ko.observable('');
}

The binding should work as normal with the exception that if input.value === input.attribute['placeholder'] then viewModel.query() === '' . 绑定应该正常工作,不同的是,如果input.value === input.attribute['placeholder']然后viewModel.query() === ''

Ug... 嗯...

Disclaimer: I don't have easy access to IE8 at the moment... 免责声明:我目前无法轻松访问IE8 ...

Best I know of, there is no easy solution to this. 据我所知,没有简单的解决方案。

I know of two options: 我知道两个选择:

Option 1: Find a different placeholder fill in 选项1:查找其他占位符

This is the approach I would take. 这就是我要采取的方法。 Find something that doesn't actually fill in the text box, but drops another element on top of it. 查找实际上没有填充文本框,但在其顶部放置另一个元素的内容。

Option 2: Use a computed 选项2:使用计算所得

If you are stuck with that particular placeholder library, then you need to create a computed value, call it query2 for now. 如果您坚持使用该特定的占位符库,则需要创建一个计算值,现在将其称为query2

In the computation function for query2 , have it access the value of query then compare it against the placeholder attribute. query2的计算函数中,让它访问query的值,然后将其与placeholder属性进行比较。 If they match, return "" , else return query 如果匹配,则返回"" ,否则返回query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM