击倒依赖可观察射击两次
我正在使用城堡单轨列车和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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与
options
绑定一起使用时,value
绑定会尝试确保该值是有效的选项。就您而言,1405 似乎不是一个有效的选择。
如果您使用的是 2.0 之前的版本,那么您的问题可能是您需要交换值/选项绑定的顺序。在2.0之前,需要先使用
options
来构建选项,然后使用value
将其设置为有效的选项The
value
binding when used with theoptions
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, thenvalue
can set it to a valid option