このプロジェクトでは JSONAPI を使用していますが、[理由] により、推奨される関係構造を API で処理できないため、次の形式のネストされたオブジェクトとしてサービスを提供し、期待しています。
{
"data":{
"type":"video",
"id":"55532284a1f9f909b0d11d73",
"attributes":{
"title":"Test",
"transcriptions":{
"type": "embedded",
"data":[
{
"type":"transcription",
"id":"203dee25-4431-42d1-a0ba-b26ea6938e75",
"attributes":{
"transcriptText":"Some transcription text here. And another sentence after it.",
"cuepoints":{
"type":"embedded",
"data":[
{
"type":"cuepoint",
"id":"bb6b0434-bdc4-43e4-8010-66bdef5c432a",
"attributes":{
"text":"Some transcription text here."
}
},
{
"type":"cuepoint",
"id":"b663ee00-0ebc-4cf4-96fc-04d904bc1baf",
"attributes":{
"text":"And another sentence after it."
}
}
]
}
}
}
]
}
}
}
}
次のモデル構造があります。
// models/video
export default DS.Model.extend({
transcriptions: DS.hasMany('transcription')
)};
// models/transcription
export default DS.Model.extend({
video: DS.belongsTo('video'),
cuepoints: DS.hasMany('cuepoint')
});
// models/cuepoint
export default DS.Model.extend({
transcription: DS.belongsTo('transcription')
);
今、私たちがしたいことは、video
レコードを保存し、それをシリアル化しtranscriptions
、cuepoints
それを含むようにすることです。私は次のシリアライザーを持っていtranscription
ますvideo
。1 つのレベルですが、そのレベルにも を埋め込む必要がありcuepoints
ます。
export default DS.JSONAPISerializer.extend({
serializeHasMany: function(record, json, relationship) {
var hasManyRecords, key;
key = relationship.key;
hasManyRecords = Ember.get(record, key);
if (hasManyRecords) {
json.attributes[key] = {};
hasManyRecords.forEach(function(item) {
json.attributes[key].data = json.attributes[key].data || [];
json.attributes[key].data.push({
attributes: item._attributes,
id: item.get('id'),
type: item.get('type')
});
});
} else {
this._super(record, json, relationship);
}
}
});
record
メソッドの、json
およびrelationship
プロパティを調べてserializeHasMany
も、ネストされたリレーションシップについて何も表示されないため、正しいメソッドを使用しているかどうかさえわかりません。
私がこれで間違っているアイデアはありますか?