Backbone.js - Correct way of filtering and displaying collection data in a view

前端 未结 3 953
栀梦
栀梦 2020-12-22 16:38

I have got a huge list of tasks loaded on the start.
I want to show them depending on selected list / inbox, so that there won\'t be additional loadings for each list.

3条回答
  •  清酒与你
    2020-12-22 17:12

    @sled there's typos in the code you posted, see comments inline. Did you post this as a project somewhere?

    // add models
    add: function(models, options) {
      // TYPO: next line was missing, so single models not handled.
      models = _.isArray(models) ? models.slice() : [models];
    
      var self = this;
    
      models = _.filter(models, this.filter);
    
      // return if no models exist
      // TYPO: returned undefined, so was not chainable
      if(models.length == 0) { return this; }
    
      // actually add the models to the superset
      this.superset.add(models, options);
      return this;
    },
    
    // remove models
    remove: function(models, options) {
      // TYPO: next line was missing, so single models not handled.
      models = _.isArray(models) ? models.slice() : [models];
    
      // remove model from superset
      this.superset.remove(_.filter(_.filter(models, function(cm) {
        // TYPO: not 'm != null', causes error to be thrown
        return cm != null;
      }), this.filter), options);
      // TYPO: missing return so not chainable
      return this;
    },
    

提交回复
热议问题