敲除包装值绑定 [英] Knockout wrap value binding
问题描述
我正在使用 mathias bynen的占位符代码,如果要删除,我想将其与我做一个简单的自定义绑定,像这样:
I am using mathias bynen's placeholder code and I wanted to use it along with knockout, if I do a simple custom binding like this:
ko.bindingHandlers.placeholder = {
init: function (element) {
$(element).placeholder();
}
};
和html
<input placeholder = "Line 1" data-bind="placeholder: {}, value: addressLine1">
它可以工作,但是我想将它们合并"到一个自定义绑定中,以便像这样使用它
It works, but I would like to "merge" them into one custom binding, for using it like
<input placeholder = "First Name" data-bind="placeholderValue: firstName">
所以我尝试了这段代码:
So I tried this code:
ko.bindingHandlers.placeholderValue = {
init: function (element, valueAccessor) {
$(element).placeholder();
ko.bindingHandlers.value.init(element, valueAccessor);
},
update: function (element, valueAccessor) {
ko.bindingHandlers.value.update(element, valueAccessor);
}
};
但是它给我带来了一个
Uncaught TypeError: undefined is not a function
我还没有真正掌握ko
I'm not really getting the grip of ko yet
推荐答案
When you are creating a delegating custom binding as a best practice you should always pass all the arguments of the init
and update
to the inner bindings, because you can never know what parameters the inner binding uses:
ko.bindingHandlers.placeholderValue = {
init: function (element, valueAccessor, allBindingsAccessor,
viewModel, bindingContext) {
$(element).placeholder();
ko.bindingHandlers.value.init(element, valueAccessor,
allBindingsAccessor, viewModel, bindingContext);
},
update: function (element, valueAccessor, allBindingsAccessor,
viewModel, bindingContext) {
ko.bindingHandlers.value.update(element, valueAccessor,
allBindingsAccessor, viewModel, bindingContext);
}
};
您有例外,因为value
出价的init
使用了allBindingsAccessor
参数,但是由于您没有在其中传递参数,因此引发了例外.
You have got the exception because the init
of the value
biding uses the allBindingsAccessor
parameter but because you haven't passed that in it raises the exception.
这篇关于敲除包装值绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!