writeValueToProperty不可用 [英] writeValueToProperty isn't available
问题描述
我正在做一个自定义绑定,我的第一个版本要求databound属性是可观察的,但是我刚刚发布了该绑定的用户可能希望使用标准属性(如果他们不希望在收到通知时被通知值已更改).
I'm doing a custom binding, my first version required the databound property to be a observable, but I just released that users of the binding probably could want to use a standard property (If they are not interested in being notified when the value has changed).
为此,我研究了值绑定是如何实现的,并使用了
To support this I looked at how the value binding is implemented, and it uses
ko.jsonExpressionRewriting.writeValueToProperty
将值同时写入可观察值或标准属性.
to write values both to observables or standard properties.
这是一个内部ko命名空间,因此我无法从绑定中使用它,我应该如何调用此方法?
This is an internal ko namespace so i cant use it from my binding, how am I supposed to call this method?
修改: 我在github上有一个pull请求来解决这个问题 https://github.com/SteveSanderson/knockout/pull/806
edit: I have a pull request at github to fix this https://github.com/SteveSanderson/knockout/pull/806
推荐答案
Knockout在allBindingsAccessor
中的结果对象中放置了一个函数,您可以使用该函数将其写入不可观察的模型值.
Knockout places a function in the resulting object from the allBindingsAccessor
that you can use to write to a non-observable model value.
如果绑定被称为myBinding
,则您的代码可能类似于:
If your binding was called myBinding
, then your code might look like:
if (ko.isObservable(modelValue)) {
modelValue(valueToWrite);
}
else { //non-observable
allBindingsAccessor()._ko_property_writers.myBinding(valueToWrite);
}
这篇关于writeValueToProperty不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!