我有这两个使用相同初始化代码的绑定,并且绑定到相同的值,但它们的初始化不同。第一个运行正常,第二个运行更新而不是 init。以下是绑定:
function initToggle(element, valueAccessor) {
// Initially set the element to be instantly visible/hidden depending on the value
var value = valueAccessor();
$(element).toggle(ko.utils.unwrapObservable(value)); // Use "unwrapObservable" so we can handle values that may or may not be observable
};
//Binding Handlers
ko.bindingHandlers.fadeVisible = {
init: initToggle,
update: function (element, valueAccessor) {
// Whenever the value subsequently changes, slowly fade the element in or out
var value = valueAccessor();
ko.utils.unwrapObservable(value) ? $(element).fadeIn() : $(element).fadeOut();
}
};
ko.bindingHandlers.slideVisibleVertical = {
init: initToggle,
update: function(element, valueAccessor, allBindingsAccessor) {
var value = valueAccessor(); //Lastest binding value
var allBindings = allBindingsAccessor(); //other bindings, allows options
var valueUnwrapped = ko.utils.unwrapObservable(value);
var duration = allBindings.slideDuration || 400; // 400ms is default duration unless otherwise specified
if (valueUnwrapped == true)
$(element).show('slide', {direction: 'up'}, duration);
else
$(element).hide('slide', {direction: 'up'}, duration);
}
};
从字面上看,相同的初始化函数。这是一个显示两个按钮的小提琴,它们都绑定到相同的值。fadeVisible
如图所示开始,但slidevisibleVertical
滑入。更新时两者都动画。有人知道这里发生了什么吗?