0

我想创建一个“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”工作了,但无法解决这个问题,我错过了什么?

4

2 回答 2

2

谢谢你的建议。我终于让它工作了,我错过了controlsDescendantBindings选项。结果如下:

ko.bindingHandlers.fadeIf = {
    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);
        return { controlsDescendantBindings: value };
    },
    update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var value = valueAccessor();
        if (ko.utils.unwrapObservable(value)) {
            ko.bindingHandlers['if'].update(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
            $(element).fadeIn();
        }
        else {
            $(element).fadeOut(200, function () {
                ko.bindingHandlers['if'].update(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
            });
        }
    }
};
于 2013-05-13T11:40:44.680 回答
0

我认为你在 99% 的时候做对了,但是在第一次之后就不会淡入了……我认为这与“if”绑定处理程序的工作方式有关。我所做的是改变这一点:

    if (ko.utils.unwrapObservable(value))
        $(element).fadeIn();

进入这个:

    if (ko.utils.unwrapObservable(value)) {
        $(element).toggle(false);
        $(element).fadeIn();
    }

如果你想检查的话,我已经保存了它的工作原理。

于 2013-05-10T13:40:37.627 回答