5

假设我有一组 3 个单选按钮:

<div>
    <label>
        <input type="radio" name="Who" value="Myself" 
            checked="@isMyselfChecked" data-bind="checked: who" />
        Mine
    </label>
    <label>
        <input type="radio" name="Who" value="MemberId" 
            checked="@isMemberIdChecked" data-bind="checked: who" />
        I know the member's ID
    </label>
    <label>
        <input type="radio" name="Who" value="MemberUrl" 
            checked="@isMemberUrlChecked" data-bind="checked: who" />
        I know the member's URL
    </label>
</div>

当用户选择第一个单选按钮(Mine/Myself)时,不需要额外的输入。但是,选择第二或第三,需要附加输入

<div>
    <input type="text" name="MemberId" placeholder="Enter Member ID" 
        data-bind="toggleWho: who()" style="display: none" />
    <input type="text" name="MemberUrl" placeholder="Enter Member URL" 
        data-bind="toggleWho: who()" style="display: none; width: 450px;" />
</div>

只需要data-bind="visible: who() === '[MemberId|MemberUrl]'"依赖文本框就很容易了。但是,如果我想添加淡入/淡出过渡怎么办?

我从淘汰网站上试用了示例自定义fadeVisible bindingHandler,并且我了解它是如何工作的。但是,这将同时淡出并淡入文本框中。如果选择了收音机'MemberId',并且用户选择'MemberUrl'了收音机,我希望MemberId文本框在文本框淡入之前MemberUrl淡出。

下面是我现在所拥有的,它可以工作,但我认为它不是最佳的。在前一个元素淡出之前,如何告诉淘汰赛不执行淡入?我需要另一个ko.observale,或者可能是一个ko.computed

var viewModel = {
    fadeSpeed: 150,
    who: ko.observable($('input[type=radio][name=Who]:checked').val())
};

ko.bindingHandlers.toggleWho = {
    init: function (element, valueAccessor) {
        var value = valueAccessor();
        var unwrapped = ko.utils.unwrapObservable(value);
        if (unwrapped === element.name)
            $(element).show();
    },
    update: function (element, valueAccessor) {
        var value = valueAccessor();
        var unwrapped = ko.utils.unwrapObservable(value);

        // when selected value is myself, fade out the visible one, if any
        if (unwrapped === 'Myself') {
            $('input[type=text][name=MemberId]:visible')
                .fadeOut(viewModel.fadeSpeed);
            $('input[type=text][name=MemberUrl]:visible')
                .fadeOut(viewModel.fadeSpeed);
        }

            // when selected value is memberid, may need to fade out url first
        else if (unwrapped === 'MemberId') {
            if ($('input[type=text][name=MemberUrl]:visible').length > 0) {
                $('input[type=text][name=MemberUrl]:visible')
                    .fadeOut(viewModel.fadeSpeed, function () {
                        $('input[type=text][name=MemberId]')
                            .fadeIn(viewModel.fadeSpeed);
                    });
            } else {
                $('input[type=text][name=MemberId]')
                    .fadeIn(viewModel.fadeSpeed);
            }
        }

            // when selected value is memberurl, may need to fade out id first
        else if (unwrapped === 'MemberUrl') {
            if ($('input[type=text][name=MemberId]:visible').length > 0) {
                $('input[type=text][name=MemberId]:visible')
                    .fadeOut(viewModel.fadeSpeed, function () {
                        $('input[type=text][name=MemberUrl]')
                            .fadeIn(viewModel.fadeSpeed);
                });
            } else {
                $('input[type=text][name=MemberUrl]')
                    .fadeIn(viewModel.fadeSpeed);
            }
        }
    }
};

ko.applyBindings(viewModel);
4

3 回答 3

9

您将不得不对其进行一些调整以适合您的示例,但我需要对其进行简化以在此 fiddle中进行测试。

这是绑定:

var previousElement = null;
ko.bindingHandlers.fadeSwitcher = {
    init: function(element, valueAccessor) {
        var value = valueAccessor();
        $(element).toggle(ko.utils.unwrapObservable(value));
    },
    update: function(element, valueAccessor) {

        var value = ko.utils.unwrapObservable(valueAccessor());
        if (value) {
            if (previousElement == null) { // initial fade
                $(element).fadeIn();
            } else {
                $(previousElement).fadeOut('fast', function() {
                    $(element).fadeIn();
                });
            }
            previousElement = element;
        }        
    }
};
于 2012-07-11T16:53:11.493 回答
1

聚会有点晚了,但也许对其他人有用。

我接受了 Tyrsius 的回答并对其进行了更改以解决我自己的需求;这个版本处理一个可观察的属性,并且每当它改变时都会淡出/淡入旧/新值。

Usage example: <span data-bind="fadeSwitcher: myObservable"></span>

ko.bindingHandlers.fadeSwitcher = {
    init: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        element.setAttribute('previousValue', value);
        ko.bindingHandlers.text.update(element, ko.observable(value));
    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        var previousValue = element.getAttribute('previousValue');
        if (value !== previousValue) {
            $(element).fadeOut('fast', function () {
                ko.bindingHandlers.text.update(element, ko.observable(value));
                $(element).fadeIn();
            });
            element.setAttribute('previousValue', value);
        }
    }
};
于 2015-08-25T08:21:30.350 回答
0

谢谢 Tyrsius 的回答。我确实不得不从小提琴中改编它。我能够使用稍微修改的绑定来使其与问题中的相同无线电一起使用(没有 foreach):

@* radios same as in question *@
<div>
    <input type="text" name="MemberId" placeholder="Enter Member ID" 
        data-bind="whoFader: who() === 'MemberId'" style="display: none" />
    <input type="text" name="MemberUrl" placeholder="Enter Member URL" 
        data-bind="whoFader: who() === 'MemberUrl'" 
        style="display: none; width: 450px;" />
</div>

ko.bindingHandlers.whoFader = {
    previousElement: null,
    init: function (element, valueAccessor) {
        var value = valueAccessor();
        $(element).toggle(ko.utils.unwrapObservable(value));
    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        if (value) {
            if (this.previousElement == null) {
                $(element).fadeIn('fast');
            } else {
                $(this.previousElement).hide();
                $(element).fadeIn('fast');
            }
            this.previousElement = element;
        }
        else {
            $(element).fadeOut('fast');
        }
    }
};
于 2012-07-11T18:28:11.250 回答