Backbone - 删除Collection中的最后一个模型,但仅限过滤器

时间:2015-05-29 14:31:16

标签: javascript backbone.js underscore.js

this.keysCollection.pop();

删除最后一个非常简单,但我理想的做法就是这个(伪解决方案):

this.keysCollection.pop({ type: 'foo' });

并从集合中删除最后一个匹配的模型。可能的?

编辑:

跟着这个 -

const models = this.where({ type: 'foo' });
const model = models[models.length - 1];
this.remove(model);

1 个答案:

答案 0 :(得分:2)

Pop真的只是获取最后一个元素,然后从backbone source

调用结果中的remove
  pop: function(options) {
      var model = this.at(this.length - 1);
      this.remove(model, options);
      return model;
    },

既然如此,您可以使用where获取与您的过滤器匹配的模型,然后将结果中的最后一个模型传递给remove

var matchingModels = this.where({type: 'foo'});
this.remove(matchingModels[matchingModels.length - 1]);