我正在尝试使用knockout inbuild WITH Binding来做简单的行为。有使用ko.applyBinding与ViewModel绑定的UL-LI列表。工作正常。 现在,我正在尝试使用list实现选择项行为。因此,在ViewModel中实现了selectPatient行为,并使用Click将其与LI标签绑定。调用该方法并设置可观察的selectedPatient变量。
现在在父级div中有一个div在UL-LI的同一级别,我试图使用With与selectedPatient绑定。不工作。
参考代码:
<div data-role="content" id="MyPatientDiv">
<div>
<ul data-bind="foreach:Patients">
<li data-bind="click:$root.selectPatient">
<table>
<tr>
<td>
<a href="javascript:" id="pName" sortorder="none"><span data-bind="text:LastName"></span>
,<span data-bind="text:$data.FirstName"></span></a>
</td>
<td>
<a href="javascript:" id="pid" sortorder="none"><span data-bind="text:$data.DisplayId">
</span></a>
</td>
<td>
<a href="javascript:" id="pLocation" sortorder="none"><span data-bind="text:$data.UnitName">
</span></a>
</td>
</tr>
</table>
</li>
</ul>
</div>
<div id="dlgAction" databind="with:selectedPatient">
What would you like to do for <span databind="text:$data.LastName"></span>,<span databind="text:$data.FirstName"></span>
</div>
</div>
脚本代码:
function PatientsModel(data) {
var self = this;
self.Patients = ko.observableArray([]);
if (data.patientEncounters == null) self.Patients([]);
else self.Patients(data);
self.selectedPatient = ko.observable(null);
self.selectPatient = function(data, target) {
self.selectedPatient(data);
//$('#dlgAction').dialog('open');
};
}
MyPatientList = {
PatientsModel: null,
DOMElementId: 'MyPatientDiv'
}
$(function(){
var data = [{
AccountId: null,
AdmissionStatus: 1
AttendingPhysicians: null,
BedId: null,
DisplayId: "32456",
EncounterKey: "00000000000000000000000000000003",
FirstName: "John",
Gender: null,
LastName: "Rambo",
LocalPatient: true,
MiddleName: null,
MyPatient: true,
PatientId: null,
PatientKey: "00000000000000000000000000000003",
RoomName: null,
SameName: false,
TemporaryPatient: false,
UnitName: "IC1",
VisitId: null}];
data[0].LastName;
MyPatientList.PatientsModel = new PatientsModel(data);
ko.applyBindings(MyPatientList.PatientsModel, $('#MyPatientDiv')[0]);
});
我做错了吗?
此致 Sumeet
答案 0 :(得分:2)
您错过了data-bind
关键字中的破折号:
<div id="dlgAction" data-bind="with:selectedPatient">
What would you like to do for <span data-bind="text:$data.LastName"></span>,
<span data-bind="text:$data.FirstName"></span>
</div>
此外,您不必将$ data放入data-bind
属性,只需指定属性名称:
<div id="dlgAction" data-bind="with:selectedPatient">
What would you like to do for <span data-bind="text: LastName"></span>,
<span data-bind="text: FirstName"></span>
</div>
编辑:它对我有用。这是一个小提琴:http://jsfiddle.net/vyshniakov/BjeP9/
数据有误 - 您在AdmissionStatus
财产后错过了昏迷。此外,您尝试加载始终为null的data.patientEncounters
,因此您的集合未初始化。