2013-08-04 22 views
0

我想顯示圖像列表及其各自的註釋。像:在一個視圖中顯示多個資源

Image url     | Format  | Comments 
http://example.com/img.jpg | 1280x420  | [Comment 1], [Comment 2] ...show all ...show all 
http://example.com/img2.jpg | 630x590  | [Comment 1], [Comment 2] ...show all 

我有兩個resouces:/圖像和/評論/ {image_id}

什麼是獲取每張圖像的意見,能夠在同一行上顯示它們推薦的方法是什麼? Marionette有這個幫手嗎?

回答

0

在我看來,這些看起來像使用關係模型的好地方。骨幹不支持這些開箱即用,所以你需要一個插件。看看Backbone-Relationalsupermodel.js。這些項目提供了比默認實現更好的模型嵌套形式。從那裏,使用嵌套複合視圖來渲染集合。

0

從我所知道的木偶沒有這樣的幫手。我想你可以用這樣簡單的東西:

var ImageComments = Backbone.Collection.extend({ 
    initialize: function(models, options) { 
    options || (options = {}); 
    this.imageId = options.imageId; 
    Backbone.Collection.prototype.initialize.apply(this, arguments); 
    }, 

    urlRoot: function() { 
    return 'comments/' + this.imageId; 
    } 
}); 

var id = 1, 
    image = new Image({ id: id }), 
    comments = new ImageComments(null, { imageId: id }); 

$.when(image.fetch(), comments.fetch()).done(function() { 
    // .. do your things with image & comments 
}); 

這說明簡單的例子,如果這是常用應用程序中使用你可能想實現自己的提取方法(如圖像,這也將提取評論)或使用插件如Backbone-relationalBackbone-associations

相關問題