淘汰赛视图模型映射停止订阅触发
Posted
技术标签:
【中文标题】淘汰赛视图模型映射停止订阅触发【英文标题】:Knockout viewmodel mapping stops subscription firing 【发布时间】:2018-07-21 09:30:42 【问题描述】:我有以下主视图模型
var masterVM = (function()
this.PatientViewModel = ko.mapping.fromJS(new PatientViewModel());
this.MedicalBenefit = new MedicalBenefitViewModel(this.PatientViewModel);
)();
以及以下视图模型
var PatientViewModel = function()
var self = this;
self.FirstName = ko.observable();
self.LastName = ko.observable();
self.NationalHealthFundId = ko.mapping.fromJS(null);
self.NationalHealthFundId.subscribe(function(newValue)
alert("subscribe from patient");
);
;
var MedicalBenefitViewModel = function (patient)
var self = this;
self.patient = patient;
;
在自定义绑定中的某处(绑定到患者):
...
var observable = valueAccessor();
ko.mapping.fromJS(patient, , observable);
...
其中患者是代表患者的简单纯 json 对象。
自定义绑定执行后,订阅(alert("subscribe from patient");)停止工作,为什么?
【问题讨论】:
【参考方案1】:您已设置self.NationalHealthFundId = ko.mapping.fromJS(null);
。你只能订阅一个 observable。例如,这有效:
var PatientViewModel = function()
var self = this;
self.FirstName = ko.observable();
self.LastName = ko.observable();
self.NationalHealthFundId = ko.observable();
self.NationalHealthFundId.subscribe(function(newValue)
alert("subscribe from patient");
);
;
var vm = new PatientViewModel()
ko.applyBindings(vm);
vm.NationalHealthFundId(1);
vm.NationalHealthFundId(2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
【讨论】:
以上是关于淘汰赛视图模型映射停止订阅触发的主要内容,如果未能解决你的问题,请参考以下文章