Filter and sort backbone collection with array of ids

前端 未结 1 1151
轻奢々
轻奢々 2021-01-13 19:18

I\'m quite new to Backbone so I am getting into some problems I can\'t quite figure out.

I have a Backbone collection with a bit over 100 items. I want to filter the

相关标签:
1条回答
  • 2021-01-13 19:55

    Backbone collections are automatically sorted by the order of insertion, unless you implement Collection#comparator. The problem is that your filtering algorithm is not producing an ordered output.

    If you need to maintain an ordered collection only when filtering by id, I would suggest implementing a separate method, because seach by id is far faster compared to search by arbitrary attributes:

    filterById: function(idArray) {
      return this.reset(_.map(idArray, function(id) { return this.get(id); }, this));  
    }
    

    Usage:

    collection.filterById(['1', '2', '146', '3', '4', '9', '26', '8', '96', '10', '11', '54',' 145', '273', '38']);
    
    0 讨论(0)
提交回复
热议问题