Backbone.js 一个视图中的多个资源

Backbone.js 一个视图中的多个资源,backbone.js,marionette,Backbone.js,Marionette,我想显示一个图像列表和它们各自的注释。比如: 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]

我想显示一个图像列表和它们各自的注释。比如:

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  
我有两个资源:/images和/comments/{image\u id}


为了能够在同一行中显示每个图像的注释,建议采用什么方法获取注释?木偶有帮手吗?

据我所知,木偶没有这样的帮手。我想你可以用一些简单的方法,比如:

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 
});

这描述了一个简单的情况,如果这在您的应用程序中常用,您可能希望实现自己的获取方法(例如,对于图像,它也将获取注释),或者使用类似或的插件,您可以使用嵌套的复合视图

您还可以对注释执行老式的模板循环


在我看来,这些看起来是使用关系模型的好地方。主干网不支持这些现成的功能,因此您需要一个插件。看一看或看一看。这些项目提供了比默认实现更好的模型嵌套形式。然后,使用嵌套的复合视图呈现集合