我想用自定義的JsonResultsAdapter連接到使用Breeze的第三方服務。JsonResultsAdapter中的嵌套實體
第三方服務具有與數組根節點中的實體相關的「元數據」,則這些變量位於「元數據」對象的「數據」屬性中。
該格式有兩種定義關係的方式。一個是通過引用另一個實體的id的「@ref」字段。另一種是通過內聯定義相關對象(而不是「@ref」),該對象沒有明確的id,但只有「parent」對象才引用該對象。
的數據看起來像:
[{
"id" : "abc",
"type" : "foo",
"data": { "relationshipRef" : { "@ref" : "someid" } }
},
{
"id": "someid",
"type" : "bar",
"data" : { "relationshipInline" : { "type" : "baz",
"data" : { "something" : "whatever",
"innerRelation" : { "@ref" : "abc"}
}
}
}]
我目前(在JsonResultsAdapter的visitNode功能)中的「數據」移動對象的屬性成「根」節點,然後用更換任何對象「@ref」屬性與「@ref」鍵的值並將一個ID附加到末尾(以便關係可以使用EntityType中的原始名稱)。 IE,第一個對象將變成:
{
"id" : "abc",
"type" : "foo",
"relationshipRefID" : "someid"
}
這適用於頂級實體和關係,但我遇到了嵌套問題。
你會如何解決這個問題?
我打算使用ComplexTypes,但文檔提到它們不能具有「navigationProperties」(關係),正如您在上面看到的那樣,它是必需的(「innerRelation」屬性)。
在某些情況下,實體可以嵌套到3層左右。
這是我目前的visitNode功能:
visitNode: function(node, parseContext, nodeContext) {
if(node instanceof Object && node.type != null) {
if(node.deleted) {
//TODO: make sure the object is removed from the manager
return {ignore:true};
}
//We need to tweak the data structure to fit what breeze expects.
//It expects properties to be in the same level as the "metadata" for an object ("type" etc),
//So we need to move the properties from the data object into the node, and fix up relationships.
if(parseContext.entityManager.metadataStore.getEntityType(node.type, true) != null) {
var data = node.data;
for(var key in data) {
var prop = data[key];
//Move any foreign key fields to be "relationID":id instead of "relation":{"@ref":id}
if(prop instanceof Object) {
var ref = prop["@ref"];
if(ref != null) {
node[key+"ID"] = ref
data[key] = null;
continue;
}
}
//TODO: Handle inline references <- This is where I need help!
node[key] = data[key];
}
return {
entityType: node.type,
nodeId: node.id
}
}
else {
return {ignore:true};
}
}
}
感謝發表回覆:) –