我已經在文檔中進行過很多次了,但這方面仍然不清楚。我認爲骨幹關係完全可以做一些它沒有的事情。基於密鑰的骨幹關係關係定義的正確方法
我正在尋找方式來定義基於關鍵的關係,以避免所有的樣板取材無稽之談。
採取規範的藝術家和專輯?例如: 一個藝術家有許多專輯由album.artist_id定義
/API /藝術家/ 62351可能返回
{
id: 62351,
name:"Jimi Hendrix"
}
類似/ API /專輯artist_id = 62351可能返回
[
{
id:5678,
name: "Are You Experienced?"
artist_id: 62351
},
{
id: 4321,
name: "Axis: Bold as love",
artist_id: 62351
}
]
我怎麼可能定義藝術家和專輯的關係,使得
var hendrixInstance = new Artist({id:62351});
hendrixInstance.get('albums');
將根據專輯foreign_key artist_id獲取並返回一個專輯集合?它必須是我還沒有嘗試的key/keySource/keyDestination的一些排列,或者是一個骨幹關係沒有試圖解決的問題,但是我的doc groking失敗了,我想這個簡潔的回答SO可能會幫助未來的Google員工。
var Artist = Backbone.RelationalModel.extend({
urlRoot: '/api/artist',
relations:[{
key: 'albums', //Docs say this is the foreign key name, but in practice it doesn't appear that way. Need keySource/Destination?
type: Backbone.HasMany,
reverseRelation: {
key: 'artist',
type: Backbone.HasOne
}
}]
});
var Album = Backbone.RelationalModel.extend({
urlRoot: '/api/album'
});
獎勵積分,引用其自身鄰接表風格PARENT_ID
謝謝@xzhang,我很高興地看到,我不是唯一一個誰預期出來的東西骨幹關係的不同。我試過你的方法,它確實有效,但經過幾次額外的迭代,我最終使用了一個變體。 – nebulous