1

我正在尝试使用淘汰赛 inbuild WITH Binding 做简单的行为。有使用 ko.applyBinding 与 ViewModel 绑定的 UL-LI 列表。工作正常。现在,我正在尝试使用列表实现选择项行为。因此在 ViewModel 中实现了 selectPatient 行为,并使用 Click 将其与 LI 标记绑定。调用该方法并设置可观察的 selectedPatient 变量。

现在在 UL-LI 同一级别的父 div 内有一个 div,我正在尝试使用 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]);
});

难道我做错了什么?

问候苏梅特

4

1 回答 1

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财产后昏迷。您还尝试加载data.patientEncounters始终为 null 的内容,因此您的集合未初始化。

于 2012-11-20T10:27:30.420 回答