这是我不确定是否有“骨干”方式来处理的常见场景之一。这是一种可能的解决方案...
当您获取文档集合 ( /api/documents) 时,您会返回一些像这样的 json:
[{id: 1, name: 'foo'}, {id: 2, name: 'bar'}]
当你获取一个文档 ( /api/documents/1) 时,返回一些像这样的 json:
{
    id: 1, name: 'foo', description: 'some stuff about foo', 
    articles: [{id: 1, name: 'hello'}, {id: 2, name: 'hi again'}]
}
当获取单个 DocumentModel 时,将有一个新属性articles。您侦听该change:articles事件,并且set该新 jsonthis.articles是 ArticlesCollection。
现在......一些代码:
var Article = Backbone.Model.extend({});
var ArticleCollection = Backbone.Collection.extend({
    model: Article
});
var Document = Backbone.Model.extend({
    constructor: function() {
        // make sure articles collection is created.
        this.articles = new ArticleCollection();
        Backbone.Model.prototype.constructor.apply(this, arguments);
    },
    initialize: function(attributes, options) {
        this.articlesChanged();
        this.on('change:articles', this.articlesChanged, this);
    },
    // articles attribute changed.
    // set it into the Collection.
    // this way, existing models will be updated, new ones added.
    articlesChanged: function(){
        // if document was fetched and has articles json array,
        // set that json into this.articles collection.
        var articles = this.get('articles');
        if(articles){
            this.articles.set(articles);
            // remove 'articles' attribute now that it is set into the collection.
            this.unset('articles');
        }
    }
});
var DocumentCollection = Backbone.Collection.extend({
    model: Document
});
更多代码:
var docs = new DocumentCollection();
docs.fetch().done(function() {
    // docs now has documents with only name, date attributes, 
    // and empty articles collection.
    var doc = docs.at(0);
    var name = doc.get('name'); // foo
    var articleCount = doc.articles.length; // 0
    doc.fetch().done(function() {
        // first doc is now full, with articles, description, etc.
        doc.articles.each(function(article) { 
            console.log(article.get('name'));
        }, this);
        // re-fetch the collection later... to check if new documents exist.
        docs.fetch().done(function() {
            // new docs added, removed docs gone.
            // existing doc models updated.
        });
    });
});
我认为我喜欢这个的主要一点是,它保留了文档/文章集合实例,即使文档集合或单个文档稍后被重新获取。
因此,例如,如果您在详细信息视图中显示 DocumentModel 及其文章集合,并且重新获取整个 DocumentCollection,则显示的 DocumentModel 在获取之后仍将在集合中(除非它实际上已在服务器)。如果你有一些change add remove类型事件连接到这些实例,这很好。
随着 Backbone 1.0 转向“更新”获取(这很棒),我很好奇是否还有其他(或更好的)解决方案,因为这确实是一个非常普遍的问题......我真的没有经常使用这个确切的解决方案,我不确定它是否理想。我曾经使用parse更多来获取子 json 和子集合reset。但我认为我在更新 fetch 时遇到了一些问题,所以开始尝试这个。