Backbone.js updating of models in a collection

后端 未结 5 1318
天涯浪人
天涯浪人 2021-01-30 11:47

Let\'s say you are building a Twitter clone with Backbone.js. You have a collection of tweets. Each tweet is obviously an instance of the Tweet model.

You create an inst

5条回答
  •  一向
    一向 (楼主)
    2021-01-30 12:34

    I believe this is what you were looking for:

    yourCollection.fetch({add: true})
    

    Then, you can bind your collection view's render to the add event:

    yourCollectionView.bind('add', this.render, this);
    

    Though, if the render is heavy, you'll want to delay it with a timeout to avoid immediately calling it for every addition to the collection:

    yourCollectionView.bind('add', this.delayedRender, this);
    
    delayedRender: function() {
      var self = this;
      if (self.renderTimer) { clearTimeout(self.renderTimer); }
      self.renderTimer = setTimeout(function() {
        self.render();
        self.renderTimer = null;
      }, 300);
    }
    

提交回复
热议问题