我想创建一个“fadeInIf”自定义绑定。我的想法相当于来自:http://knockoutjs.com/examples/animatedTransitions.html 的“fadeVisible ”:
// Here's a custom Knockout binding that makes elements shown/hidden via jQuery's fadeIn()/fadeOut() methods
// Could be stored in a separate utility library
ko.bindingHandlers.fadeVisible = {
init: function(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
},
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.fadeInIf = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var value = valueAccessor();
$(element).toggle(ko.utils.unwrapObservable(value));
ko.bindingHandlers.if.init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var value = valueAccessor();
if (ko.utils.unwrapObservable(value))
$(element).fadeIn();
ko.bindingHandlers.if.update(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
}
};
我已经让“fadeVisible”工作了,但无法解决这个问题,我错过了什么?