我试图弄清楚是否可以将两个observable绑定到一个自定义绑定。以下代码段的工作方式与我期望的单个可观察量一样:
标记:
Column:
<select data-bind="options: $root.ColumnDetails,
value: column,
optionsText: 'Name',
values: Name,
optionsCaption: 'Select Column...'"></select>
Operator:
<select data-bind="options: $root.Operators,
value: operator,
optionsText: 'DisplayName',
values: Name,
optionsCaption: 'Select Operator...'"></select>
Values:
<input type="text" data-bind="expressionValues: operator">
JavaScript的:
ko.bindingHandlers.expressionValues = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
// do stuff
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
// do stuff
}
}
上面的代码按预期工作,当运算符下拉列表的值发生更改时触发自定义绑定的更新事件。但是,我想要的结果是将两个选择绑定到绑定处理程序,并在更改任何一个时触发更新。
我尝试以这种方式进行设置,但更新事件不会触发:
Values:
<input type="text" data-bind="expressionValues: [ operator, column ]">
关于如何实现这一目标的任何想法?
答案 0 :(得分:0)
您可以尝试将带有observables的对象传递给您的绑定,如下所示:
<input type="text" data-bind="expressionValues: { valueOperator: operator, valueColumn }">
并在绑定处理程序函数中通过valueAccessor
引用它们:
ko.bindingHandlers.expressionValues = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var parameters = ko.utils.unwrapObservable(valueAccessor());
var operator = ko.utils.unwrapObservable(settings.valueOperator);
var column = ko.utils.unwrapObservable(settings.valueColumn);
// do stuff
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var parameters = ko.utils.unwrapObservable(valueAccessor());
var operator = ko.utils.unwrapObservable(settings.valueOperator);
var column = ko.utils.unwrapObservable(settings.valueColumn);
// do stuff
}
}