简体   繁体   English

敲除自定义绑定扩展了“ IF”绑定

[英]Knockout custom binding extending the “IF” binding

I have the following custom binding based on Ryan Niemeyer's blog but I was it doesn't work. 我有以下基于Ryan Niemeyer博客的自定义绑定,但我认为它不起作用。 Instead of fading him the div just doesn't show at all. div没有消失,没有消失。 I tried adding the "init" function but that did not resolve the issue so I went back to just the simple update function like Ryan has it in the example. 我尝试添加“ init”函数,但未能解决问题,因此我回到了简单的更新函数,例如Ryan在示例中使用了它。

ko.bindingHandlers.fadeInIf = {
    update: function(element, valueAccessor) {
        ko.bindingHandlers.if.update(element, valueAccessor);
        $(element).fadeIn(); 
    } 
};

in the html I do the following: 在html中,我执行以下操作:

<div data-bind="fadeInIf: show">...</div>

Blog post: http://www.knockmeout.net/2011/07/another-look-at-custom-bindings-for.html 博客文章: http : //www.knockmeout.net/2011/07/another-look-at-custom-bindings-for.html

The if binding you are proxying to in your fadeInIf is actually used for creating dom elements, see this documentation for details. fadeInIf中代理的if绑定实际上用于创建dom元素,请参阅此文档以获取详细信息。

To achieve a fadeInIf you simple need. 如果您简单需要,可以实现fadeIn。

ko.bindingHandlers.fadeInIf = {
    update: function(element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        if (value) {
            $(element).fadeIn();
        }
    }
};

http://jsfiddle.net/madcapnmckay/3rRUQ/2/ http://jsfiddle.net/madcapnmckay/3rRUQ/2/

If what you want is more of a fadeVisible binding I have included an example of that also in the fiddle. 如果您想要的更多是fadeVisible绑定,我也将在提琴中提供了一个示例。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM