我无法使用 Knockoutjs 绑定将更新保存到 Breezejs 中的现有数据图。
我从使用 HotTowel 和 Durandal 的 John Papa 的 SPA 开始。
症状是如果更新是医生的简单属性,我可以将更新保存到医生记录。但是,如果我在包含在医生中的一个集合中添加一个新元素,我可以保存集合元素,但它会到达服务器并保存到数据库中,但具有空属性并且没有对医生的引用。我以 Specialty 为例,但我看到在医师记录中添加项目的任何集合都有相同的行为。添加的项目以所有属性中的空值出现在服务器上。
医生图表在页面上显示良好,我可以在适当的情况下将下拉菜单与特定字段的适当值相关联。复选框等也显示适当的值。
这是我正在使用的一段 HTML:
<div id="physGraph" class="span10" data-bind="with: currentPhysician()[0]">
<!-- more stuff here -->
<div id="physSpecialties" class="span8">
<span class="span8">Physician Specialties<i class="icon-plus-sign" title="Add new specialty to physician" data-bind="click: $parents[0].addSpecialtyToPhysician"></i></span>
<div class="span8 table-bordered" data-bind="foreach: physicianSpecialties">
<div class="span8">
<span data-bind="text: specialty().name()"></span>
</div>
<div class="span7 table-bordered">
<i class="icon-remove-sign" title="Remove specialty from physician list" data-bind="click: $parents[1].removeSpecialtyFromPhysician"></i>
<select data-bind="options: $parents[1].specialties(), optionsText: 'name', value: specialty()"></select>
</div>
</div>
</div>
<!-- more stuff here -->
</div> <!-- end with: currentPhysician()[0]
以下是我找医生的方式:
define([services/datacontext', 'viewmodels/physList'], function (datacontext, physListVm) {
var initialized = false;
var physNotes = ko.observableArray();
var currentPhysician = ko.observable();
var vm = {
activate: activate,
title: 'Physician Edit',
currentPhysician: currentPhysician,
addSpecialtyToPhysician: addSpecialtyToPhysician,
save: save,
// more code in here
}
// internal functions for physDetailEdit
function activate() {
initLookups();
var physIdSelectedFromList = physListVm.selectedPhys().id();
return getPhysicianDetail(physIdSelectedFromList);
}
function getPhysicianDetail(requestedPhysId) {
var promise = datacontext.getPhysicianDetails(requestedPhysId, currentPhysician);
return promise;
}
more code in here
}
这是我添加专业的方式:
function addSpecialtyToPhysician(item, event) {
var newItem = datacontext.createSpecialty();
item.physicianSpecialties.push(newItem);
// I've also tried it like this -- > currentPhysician()[0].physicianSpecialties.push(newItem);
}
这是我保存记录的方式:
function save() {
return datacontext.saveChanges();
}
下面是这两项的codeFirst描述:
public class Physician
{
public Physician()
{
PhysicianSpecialties = new List<PhysicianSpecialty>();
PhysicianPayers = new List<PhysicianPayer>();
IncentivePrograms = new List<PhysicianIncentiveDetail>();
PhysicianNotes = new List<Note>();
PhysInOrgs = new List<PhysInOrg>();
Memberships = new List<Membership>();
}
public Int32 Id { get; set; }
public Person ContactInfo { get; set; }
public ICollection<Membership> Memberships { get; set; }
public ICollection<PhysicianIncentiveDetail> IncentivePrograms { get; set; }
public ICollection<PhysicianPayer> PhysicianPayers { get; set; }
public ICollection<PhysicianSpecialty> PhysicianSpecialties { get; set; }
public ICollection<Note> PhysicianNotes { get; set; }
public ICollection<PhysInOrg> PhysInOrgs { get; set; }
public string Dea { get; set; }
public string Npi { get; set; }
public string Tin { get; set; }
public string Ssn { get; set; }
public string TaxId { get; set; }
public string MedLicenseNbr { get; set; }
public string MedLicenseState { get; set; }
public DateTime? MedLicenseRecertDate { get; set; }
public EMRSystem EmrSystem { get; set; }
public int ImportBatchId { get; set; }
public bool IsPcp { get; set; }
public bool SoloPractitioner { get; set; }
public bool PartOfHospital { get; set; }
}
public class PhysicianSpecialty
{
public Int32 Id { get; set; }
public Physician Physician { get; set; }
public Specialty Specialty { get; set; }
public bool IsPrimary { get; set; }
}
我保存时得到的是:
Id IsPrimary Physician_Id Specialty_Id
21 0 NULL NULL
我假设通过向医师图表添加专业,当我将专业推送到 currentPhysician()[0] 时,Breeze 将自动从页面上出现的下拉列表中插入医师 ID 和所选专业。
谁能看到我错过了什么?
谢谢