【问题标题】:Breeze not populating entities from navigationPropertiesBreeze 没有从 navigationProperties 填充实体
【发布时间】:2014-05-30 04:22:18
【问题描述】:

我有客户端定义的元数据,如下所示:

helper.addTypeToStore(manager.metadataStore, {
  name: 'PriorStudy',
  dataProperties: {
    priorStudyId: { type: breeze.DataType.Int32 },
    priorStudyType: { max: 6 },
    priorStudyPurpose: { max: 12 },
    notes: { max: 250 }
  }
});

helper.addTypeToStore(manager.metadataStore, {
  name: 'Patient',
  dataProperties: {
    patientId: { type: breeze.DataType.Int32 },
    firstName: { max: 25 },
    lastName: { max: 25 },
  },
  navigationProperties: {
    priorStudies: { entityTypeName: 'PriorStudy', fk: ['patientId'], hasMany: true}
  }
})    

来自服务器的 JSON 响应的形状如下:

{ FirstName: "Steve",
  LastName: "Holt",
  PatientId: 1,
  PriorStudy: [
    {
      PriorStudyId: 1,
      PriorStudyType: "BLAH",
      PriorStudyPurpose: "Whatever",
      PatientId: 1,
      Notes: "la la la"
    }
  ]
}

我正在测试创建这样的实体:

var query = breeze.EntityQuery.from('Patient/1').toType('Patient')
console.log(query);
manager.executeQuery(query).then(function (results) {
  console.log(results);
})
.then(function () {
  var ents = breeze.EntityQuery.from('PriorStudy').using(manager).executeLocally();
  console.log(ents);
});

Patient 实体按预期创建,但无论我尝试什么,PriorStudy 实体都是空的。我即将求助于编写 JsonResultsAdapter。

我知道这听起来很像其他问题,但两天来我一直在努力解决这个问题,不知道还能尝试什么。任何建议将不胜感激。

编辑:链接到非工作代码 https://gist.github.com/dlmanning/c09fe225995bc7cb682b

【问题讨论】:

    标签: breeze


    【解决方案1】:

    你在这里遗漏了一些东西。

    1 - 您错误地重新定义了客户端元数据。您的“PriorStudy”实体类型的正确定义应如下所示。

        name: 'PriorStudy',
        dataProperties: {
           priorStudyId: { type: breeze.DataType.Int32 },
           patientId: { type: breeze.DataType.Int32 },
           priorStudyType: { max: 6 },
           priorStudyPurpose: { max: 12 },
           notes: { max: 250 }
        },
    
        navigationProperties: {
          patient: 'Patient',
        }
    

    请注意“患者 ID”外键以及“患者”标量导航属性。

    那么您的“患者”实体类型的定义如下所示。

        name: 'Patient',
        dataProperties: {
          patientId: { type: breeze.DataType.Int32 },
          firstName: { max: 25 },
          lastName: { max: 25 },
        },
    
        navigationProperties: {
          priorStudies: { entityTypeName: 'PriorStudy', hasMany: true }
        }
    

    请注意,您没有在“Patient”实体类型上指定“priorStudies fk”属性。

    2 - 正确定义元数据后,在您的查询中,您可以使用“expand”方法查询您的患者,同时从数据库中获取“PriorStudies”。

    var query = breeze.EntityQuery.from('Patient/1') .toType('Patient') .expand('priorStudies');

    编辑 1:

    添加Plunkr

    编辑 2:

    除了定义(和修复)客户端元数据之外,您还必须编写自己的自定义 JsonResultsAdapter。在Breeze Samples 页面上查找 Edmunds 和 ESPN 示例。 Breeze mapping json 教程也可能很有帮助。

    这是我创建的gist,用于说明如何编写自定义jsonResultsAdapter。寻找createJsonResultsAdapter 方法。

    希望这会有所帮助。

    【讨论】:

    • 谢谢!现在尝试加载数据时出现错误:TypeError: undefined is not a function at updateRelatedEntityInCollection (http://localhost:1337/js/lib/breeze.debug.js:14542:40) 在调试器中看起来,relatedEntity 似乎是一个原始对象(因此缺少 getProperty 方法),而不是被包装为实体。有什么想法吗?
    • 除非我看到一些代码,否则很难说。我添加了我的 plunkr 链接来演示这应该如何工作。也许您可以查看并比较您所做的不同之处。
    • 抱歉这么久才回复。不得不做其他事情。因此,我注意到在第 108-110 行的 Plunkr 示例中,您明确地做了我试图隐式发生的事情:创建 PriorStudy 实体并将其添加到 Patient 实体。需要明确的是:我正在从服务器获取 JSON 格式的整个对象图,并试图轻松地将整个事物解析到实体存储中。非常感谢您的帮助。
    • 我添加了一个指向原始帖子的链接,其中包含我无法运行的代码。它看起来很糟糕,但这只是我试图自学微风的工作原理。
    • 对不起。我完全错过了您无法控制原始帖子中服务器的 json 响应。你是对的。除了定义(和修复)客户端元数据之外,您还必须编写自己的自定义 JsonResultsAdapter。在breezejs.com/samples 上查找 Edmunds 和 ESPN 样本。此链接breezejs.com/documentation/mapping-json 也可能会有所帮助。与此同时,我将努力更新我的帖子以显示更完整的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多