击倒依赖可观察射击两次

发布于 2024-12-29 06:04:48 字数 1066 浏览 0 评论 0原文

我正在使用城堡单轨列车和NVelocity 视图引擎。我有以下模型:

    var sampleModel ;

    jQuery(function () {
        var mappings = {
            'DateSent': ko.utils.dateConversionFunc()
        };

        sampleModel = {
            dto: ko.mapping.fromJS($dto, mappings),
            ReasonOtherId: $reasonOtherId,
            referralReasonOptions: $reasonOptions //$reasonOptions is a Json list
        };
        sampleModel.showOtherReason = ko.dependentObservable(function () {
            alert(this.dto.referralReason());
            return this.dto.referralReason() == this.ReasonOtherId;
        }, sampleModel);
        ko.applyBindings(sampleModel, jQuery('#referralContainer')[0]);
    }
    );

select data-bind="value : dto.referralReason, options: ReferralReasonOptions.Options, optionsText: 'DisplayName', optionsValue:'Id'">

如果 dto.referralReason (或 $dto)为空,则 SampleModel.showOtherReason 将触发一次并提醒 Id。奇怪的是,如果 $dto 不为空,sampleModel.showOtherReason 执行两次并弹出两个警报,第一个警报显示“1405”,这是正确的,但随后它会触发另一个“未定义”的警报。如果有任何数据,有人知道它会发射两次吗?谢谢。

I'm using Castle Monorail & NVelocity View Engine. I have the following model:

    var sampleModel ;

    jQuery(function () {
        var mappings = {
            'DateSent': ko.utils.dateConversionFunc()
        };

        sampleModel = {
            dto: ko.mapping.fromJS($dto, mappings),
            ReasonOtherId: $reasonOtherId,
            referralReasonOptions: $reasonOptions //$reasonOptions is a Json list
        };
        sampleModel.showOtherReason = ko.dependentObservable(function () {
            alert(this.dto.referralReason());
            return this.dto.referralReason() == this.ReasonOtherId;
        }, sampleModel);
        ko.applyBindings(sampleModel, jQuery('#referralContainer')[0]);
    }
    );

select data-bind="value : dto.referralReason, options: referralReasonOptions.Options, optionsText: 'DisplayName', optionsValue:'Id'">

If the dto.referralReason (or $dto) is empty , the sampleModel.showOtherReason will fires once and alert the Id. The strange thing is, if $dto is NOT empty, sampleModel.showOtherReason execute twice and two alert pop out, the first alert shows '1405', which is correct, but then it fires another alert which is "undefined". Does anyone know it is firing twice if there is any data? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春夜浅 2025-01-05 06:04:48

options 绑定一起使用时,value 绑定会尝试确保该值是有效的选项。

就您而言,1405 似乎不是一个有效的选择。

如果您使用的是 2.0 之前的版本,那么您的问题可能是您需要交换值/选项绑定的顺序。在2.0之前,需要先使用options来构建选项,然后使用value将其设置为有效的选项

The value binding when used with the options binding tries to ensure that the value is a valid option.

In your case, it looks like 1405 is not a valid choice.

If you are using a version prior to 2.0, then your issue is likely that you need to swap the order of your value/options bindings. Prior to 2.0, options needs to come first to build the options, then value can set it to a valid option

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文