1

所以我有几种类型的数据:

Post Project Event

这些数据模型中的每一个都有自己的集合和查看它们的路径:

/posts    => app.postsCollection
/projects => app.projectsCollection
/events   => app.eventsCollection

现在我想添加另一条路线:

/ => app.everythingCollection

如何创建一个显示其他三个集合的聚合的集合,但又不获取所有后期项目和事件数据

类似地,调用everythingCollection.fetch()将填充postsCollection,以便它们的数据在独立呈现时可用。projectsCollectioneventsCollection

重点是永远不要两次下载相同的数据。

4

4 回答 4

1

app.everythingCollection不必是一个真正的骨干收藏。它所需要的只是访问获取其他集合。

您也可以继承Backbone.Events来获得所有活动设施。

var fetchedRecords = {posts: 0, projects: 0, events: 0};
var Everything = function () {}
_.extend(Everything.prototype, Backbone.Events, {
    fetch: function (option) {
      that = this;
      this.count = 0;
      option.success = function () {that.doneFetch(arguments)};
      if (fetchRecords.posts == 0) {
        option.fetchedName = "posts";
        app.postsCollection.fetch(option);
        this.count ++;
      }
      if (fetchRecords.projects == 0) {
        option.fetchedName = "projects";
        app.projectsCollection.fetch(option);
        this.count ++;
      }
      if (fetchRecords.events == 0) {
        option.fetchedName = "events";
        app.eventsCollection.fetch(option);
        this.count ++;
      }
    },
    donefetch: function (collection, response, options) {
      if (this.count <=0) return;
      this.count --;
      if (this.count == 0) {
        if (options.reset) this.trigger("reset");
      }
      fetchedRecords[options.fetchedName] ++;
    },
    posts: function () {return app.postsCollection},
    projects: function () {return app.projectsCollection},
    events: function () {return app.eventsCollection}
});

app.everythingCollection = new Everything;
everythingView.listenOn app.everythingCollection, "reset", everythingView.render;
app.everythingCollection.fetch({reset: true});

您将需要增加fetchedRecrods计数以防止多次获取。像这样的东西。代码未经测试。但想法是一样的。

于 2013-11-11T07:18:12.457 回答
0

听起来像braddunbar 的超级名模benvinegar 的骨干。uniquemodel可能会解决您的问题

在接下来的构建 Soundcloud中,还值得查看Soundcloud 的文章(请参阅在视图之间共享模型)。他们在解决这个问题时与上述两个插件有类似的方法。

于 2013-11-15T14:28:45.967 回答
0
var EverythingCollection = Backbone.Model.extend({

customFetch: function (){

    var collections = [app.postsCollection, app.projectsCollection, app.eventsCollection],
        index = -1,
        collection,
        that = this;

    this.reset(); //clear everything collection.

    //this function check collections one by one whether they have data or not. If collection don't have any data, go and fetch it.
    function checkCollection() {            
        if (index >= collections.length) { //at this point all collections have data.
            fillEverything();
            return;
        }

        index = index + 1;            
        collection = collections[index];

        if (collection && collection.models.length === 0) { //if no data in collection.
            collection.fetch({success: function () {
                checkCollection();
            }}); 
        } else { //if collection have data already, go to next collection.
            checkCollection();
        }
    }

    function fillEverything() {
        collections.forEach(function (collection) {                
            if (collection) {
                that.add(collection.models); //refer this http://backbonejs.org/#Collection-add
            }
        });
    }
 }
});

像下面这样使用。

app.everythingCollection = new EverythingCollection();
app.everythingCollection.customFetch();

对于其他集合,请models在获取数据之前检查长度。像下面的东西。

if (app.postsCollection.models.length === 0) {
    app.postsCollection.fetch();
}
于 2013-11-07T09:23:48.200 回答
0

在应用程序启动时将所有必要的集合存储在数组或对象中,将事件侦听器附加到每个集合以侦听第一个重置事件,并记住您在第二个数组中获取的那些。如果使用了需要所有集合的路由,则可以为已获取的集合获取数组中未找到的路径:

(未经测试,但它会让你知道我应该怎么做)

var allCollections = [app.postsCollection, app.projectsCollection, app.eventsCollection];
var fetchedCollections = [];

$.each(allCollection, function(i, coll){
  coll.once("reset", function(){
    fetchedCollections.push(coll);
  })
});

var fetchAll = function(){
    $.each(allCollections, function(i, coll){
       if( $.inArray(coll, fetchedCollections ) == -1 ){
         coll.fetch();
       }
    });
}

在您的everythingCollection 中执行此操作,您就拥有了所需的everythingCollection.fetchAll() 功能。您还可以覆盖 EverythingCollection 的 fetch 函数以首先获取所有其他集合:

fetch(options){
  this.fetchAll();
  return Backbone.Collection.prototype.fetch.call(this, options);
}
于 2013-11-09T08:43:14.930 回答