2
votes

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.

1
Can you share what referralReasonOptions looks like? 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. Also, what version of KO are you using? If you are not using 2.0, then you will want to swap the order of your value/option bindings. Prior to 2.0, options needs to come first to build the options, then value can set it to a valid option. - RP Niemeyer
FYI, im using knockout-1.2.1 . Ok, let me try it now - TJ.
Thanks @RPNiemeyer , it works now. Options should come first. Thanks alot - TJ.
@RPNiemeyer , can you please comment it and i will mark your answer. Thanks - TJ.

1 Answers

4
votes

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