我使用backbone.js和骨幹關係0.5.0與Rails 3.2後端。我有一個卡模型has_many註釋。骨幹關係抓取相關不發送請求
這裏是我JS模型和集合:
Workflow.Collections.Cards = Backbone.Collection.extend({
model: Workflow.Models.Card,
url: '/cards'
});
Workflow.Models.Card = Backbone.RelationalModel.extend({
modelName : 'card',
urlRoot : '/cards',
relations: [
{
type: Backbone.HasMany,
key: 'notes',
relatedModel: 'Workflow.Models.Note',
collectionType: 'Workflow.Collections.Notes',
includeInJSON: false,
reverseRelation: {
key: 'card',
includeInJSON: 'id'
}
}]
});
Workflow.Collections.Notes = Backbone.Collection.extend({
model: Workflow.Models.Note,
url: '/cards/74/notes' // intentionally hard-coded for now
});
Workflow.Models.Note = Backbone.RelationalModel.extend({
modelName : 'note',
urlRoot : '/notes'
});
正常取的偉大工程,但是當我嘗試fetchRelated在控制檯中,我得到一個空數組:
card = new Workflow.Models.Card({id: 74}) // cool
card.fetch() // hits the sever with GET "/cards/74" - works great
card.fetchRelated('notes') // [] - didn't even try to hit the server
有什麼奇怪的是,這個工程:
card.get('notes').fetch() // cool - GET "/cards/74/notes"
我能使用該方法並解析響應文本,但感覺真的很髒。
任何人都知道我在這裏失蹤?
在此先感謝,這真是折磨我!
斯圖
謝謝,我會在我到我的電腦時試試這個!我能問一下爲什麼給卡片一些筆記開始服務?我試圖懶加載這些數據,而不是在頁面加載時將其關閉,並且我引導了卡片等等,所以很有可能我的卡片JS模型在我獲取它們之前不會意識到他們的筆記... – Stu
好吧,進度。這會爲我們指定的每個音符引發GET。任何想法,如果我可以使用fetchRelated完全懶加載筆記?我想從服務器上拉下新的... – Stu
好的,我明白你的觀點。我窺視了主幹關係源,並且我認爲如果沒有相應的模型ID數組,就不能使用'fetchRelated'。順便說一句,你可以看看[單元測試] [1]的例子,使用'fetchRelated'。 因此,當您獲取一個'Card'並調用'fetchRelated'時,您可以獲得'Note' ids數組。或者,也許你不喜歡這樣做? [1]:https://github.com/PaulUithol/Backbone-relational/blob/master/test/tests.js#L563 – theotheo