【问题标题】:backbone - how to trigger render of view after fetching all models in a collection骨干 - 如何在获取集合中的所有模型后触发视图渲染
【发布时间】:2014-04-17 07:12:48
【问题描述】:

我对集合中的模型进行了循环,以使用集合描述中的函数 fetchAll 获取。

fetchAll: function(){
    this.counter=0;
    self = this;
      for (var i=0;i<this.models.length; i++){
      this.models[i].fetch({
        success: function(){
          self.counter +=1;
          if (self.counter == self.models.length){
            alert('done');
            self.doneFetchAll = true;
          }
        }
      });

      //console.log(i);
      }

获取完成后,我看到一个警报,并且集合属性doneFetchAll 设置为true....但是完成后如何触发视图的渲染?

1) 主干中是否有可能监听集合的特定属性的变化,如果是肯定的,再次调用渲染?

2) 有没有更好的方法来获取集合中的所有模型,然后重新渲染视图?

所有这些监听变化的努力都失败了(whiting the initialize: function() of the view):

this.listenTo(this.collection, "change:doneFetchAll", this.render);

this.collection.on("change:doneFetchAll", this.render, this);

谢谢。

【问题讨论】:

    标签: javascript backbone.js


    【解决方案1】:

    尝试使用自定义事件:

    fetchAll: function(){
        this.counter=0;
        self = this;
          for (var i=0;i<this.models.length; i++){
          this.models[i].fetch({
            success: function(){
              self.counter +=1;
              if (self.counter == self.models.length){
                self.trigger('fetchAll'); // here
                self.doneFetchAll = true;
              }
            }
          });
    
      //console.log(i);
      }
    

    然后在initialize: function():

    this.listenTo(this.collection, "fetchAll", this.render);
    

    【讨论】:

      【解决方案2】:

      你不能只听“同步”事件而不是 fetchAll 吗?

      this.listenTo(this.collection, "sync", this.render)?
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        • 1970-01-01
        • 1970-01-01
        • 2016-01-23
        • 1970-01-01
        相关资源
        最近更新 更多