2012-06-06 54 views
3

我使用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" 

使用該方法並解析響應文本,但感覺真的很髒。

任何人都知道我在這裏失蹤?

在此先感謝,這真是折磨我!

斯圖

回答

0

我應該張貼我的解決辦法而回 - 可能會有更好的方式,但這是我已經過去的慣例:

以下所有代碼都在卡片視圖(這是顯示註釋的地方)。

首先,我綁定一個renderNotes方法對卡的票據集合'reset'事件:

initialize: function() { 
    _.bindAll(this); 

    this.model.get('notes').on('reset', this.renderNotes); 

    var self = this; 
    this.model.get('notes').on('add', function(addedNote, relatedCollection) { 
     self.renderNote(addedNote); 
    }); 
    } 

我也綁定到'add'上收集調用奇異renderNote

的renderNotes和renderNote方法的工作方式是這樣的:

renderNotes: function() { 
    if (this.model.get('notes')) { 
     this.model.get('notes').each(this.renderNote); 
    } 
    }, 

    renderNote: function (note) { 
    var noteView = new Workflow.Views.Note({ model: note }); 
    this.$('.notes').append(noteView.render().el); 
    }, 

然後,拼圖的最後一塊是實際點擊服務器了該卡的筆記(這將反過來火災'reset'事件中,我結合到上面)。我這樣做的卡片視圖的render方法:

render: function() { 
    // render all of the eager-loaded things 
    this.model.get('notes').fetch(); 
    return this; 
    }, 

正如@ user1248256好心幫我在我的OP的意見制定出的混亂主要是在我預料fetchRelated拉下延遲加載的記錄 - 這是實際上並非如此。

作爲一個側面說明,這個視圖實際上是一個模式,可以打開和關閉(從頁面中刪除)。爲了防止this excellent post中描述的殭屍事件問題,我還手動解除了上述事件。

2

您應該創建CardNote ids數組:card = new Workflow.Models.Card({id: 74, notes: [74, 75]});並相應地改變Notesurl方法:

Workflow.Collections.Notes = Backbone.Collection.extend({ 
    model: Workflow.Models.Note 
}); 

Workflow.Models.Note = Backbone.RelationalModel.extend({ 
    modelName : 'note', 
    urlRoot  : function() { 
    return this.get('card').url() + '/notes'; 
} 
}); 

card = new Workflow.Models.Card({id: 74, notes: [74, 75]}); 
card.fetchRelated('notes'); 

http://jsfiddle.net/theotheo/5DAzx/

+0

謝謝,我會在我到我的電腦時試試這個!我能問一下爲什麼給卡片一些筆記開始服務?我試圖懶加載這些數據,而不是在頁面加載時將其關閉,並且我引導了卡片等等,所以很有可能我的卡片JS模型在我獲取它們之前不會意識到他們的筆記... – Stu

+0

好吧,進度。這會爲我們指定的每個音符引發GET。任何想法,如果我可以使用fetchRelated完全懶加載筆記?我想從服務器上拉下新的... – Stu

+0

好的,我明白你的觀點。我窺視了主幹關係源,並且我認爲如果沒有相應的模型ID數組,就不能使用'fetchRelated'。順便說一句,你可以看看[單元測試] [1]的例子,使用'fetchRelated'。 因此,當您獲取一個'Card'並調用'fetchRelated'時,您可以獲得'Note' ids數組。或者,也許你不喜歡這樣做? [1]:https://github.com/PaulUithol/Backbone-relational/blob/master/test/tests.js#L563 – theotheo