我正在尝试使用breeze.js配置一些模型
第一个是会话,我能够通过下面的简单查询完美地完成这个工作
var query = breeze.EntityQuery.from("sessions").toType("Session");
..但是当我尝试在会话中添加一个相关的“扬声器”数组时,我在物化步骤完成后似乎留下了一个空数组
我是否需要为这样的api编写自定义json适配器?
/api/sessions/
[
{
"id": 1,
"name": "javascript",
"speakers": [
1
]
}
]
/api/speakers/1/
{
"id": 1,
"name": "Toran",
"session": 1
}
这是我当前的模型配置(主要是工作)
var ds = new breeze.DataService({
serviceName: 'api',
hasServerMetadata: false,
useJsonp: false
});
breeze.config.initializeAdapterInstance("modelLibrary", "backingStore", true);
this.instance = new breeze.EntityManager({dataService: ds});
this.instance.metadataStore.addEntityType({
shortName: "Speaker",
namespace: "App",
dataProperties: {
id: { dataType: "Int64", isPartOfKey: true },
name: { dataType: "String" },
session: { dataType: "Int64" }
},
navigationProperties: {
session: {
entityTypeName: "Session", isScalar: true,
associationName: "session", foreignKeyNames: ["session"]
}
}
});
this.instance.metadataStore.addEntityType({
shortName: "Session",
namespace: "App",
dataProperties: {
id: { dataType: "Int64", isPartOfKey: true },
name: { dataType: "String" },
speakers: { dataType: "Undefined" }
},
navigationProperties: {
speakers: {
entityTypeName: "Speaker", isScalar: false,
associationName: "speakers", foreignKeyNames: ["speakers"]
}
}
});
感谢您的帮助!
注意**我正在使用ember.js(非骨干/ ko / angular)
我试图保持上述简介,但如果你完全需要一切,请查看这个单一的js文件
https://github.com/toranb/embereeze/blob/master/website/static/website/js/app.js
更新
如果我删除“speaker”dataProperty breeze在这段代码中相当不满意(因为它不是会话模型上的dataProperty)
var fkProps = fkNames.map(function (fkName) {
//fkName is speakers .... yet it's not a data property so bad things happen :(
return parentEntityType.getDataProperty(fkName);
});
var fkPropCollection = parentEntityType.foreignKeyProperties;
// Array.prototype.push.apply(parentEntityType.foreignKeyProperties, fkProps);
fkProps.forEach(function (dp) {
答案 0 :(得分:1)
我刚做了一个公关 - 两端的关联名称应该是相同的,就像任何关系数据库一样。
this.instance.metadataStore.addEntityType({
shortName: "Speaker",
namespace: "App",
dataProperties: {
id: { dataType: "Int64", isPartOfKey: true },
name: { dataType: "String" },
session: { dataType: "Int64" }
},
navigationProperties: {
sessionModel: {
entityTypeName: "Session", isScalar: true,
associationName: "Speaker_Sessions", foreignKeyNames: ["session"]
}
}
});
this.instance.metadataStore.addEntityType({
shortName: "Session",
namespace: "App",
dataProperties: {
id: { dataType: "Int64", isPartOfKey: true },
name: { dataType: "String" }
},
navigationProperties: {
speakers: {
entityTypeName: "Speaker", isScalar: false,
associationName: "Speaker_Sessions"
}
}
});
所以我重命名了关联,删除了Session上的额外speakers
属性(没有关系的属性,只是应该映射的反转),然后在你没有删除会话上的外键那里不需要那个。通过使用上面显示的模型从API分配传递session
应该足以让Breeze知道