Backbone - 删除集合中的最后一个模型,但仅通过过滤器
Backbone - remove last model in Collection, but only by filter
this.keysCollection.pop();
删除最后一个非常简单,但我最想做的是这个(伪解决方案):
this.keysCollection.pop({ type: 'foo' });
并从集合中删除最后一个匹配模型。可能吗?
编辑:
用这个 -
const models = this.where({ type: 'foo' });
const model = models[models.length - 1];
this.remove(model);
Pop 实际上只是从 backbone source
获取最后一个元素,然后对结果调用 remove
pop: function(options) {
var model = this.at(this.length - 1);
this.remove(model, options);
return model;
},
既然如此,您可以自己使用where to get the models that match your filter and then pass the last model from the results into remove。
var matchingModels = this.where({type: 'foo'});
this.remove(matchingModels[matchingModels.length - 1]);
this.keysCollection.pop();
删除最后一个非常简单,但我最想做的是这个(伪解决方案):
this.keysCollection.pop({ type: 'foo' });
并从集合中删除最后一个匹配模型。可能吗?
编辑:
用这个 -
const models = this.where({ type: 'foo' });
const model = models[models.length - 1];
this.remove(model);
Pop 实际上只是从 backbone source
获取最后一个元素,然后对结果调用 remove pop: function(options) {
var model = this.at(this.length - 1);
this.remove(model, options);
return model;
},
既然如此,您可以自己使用where to get the models that match your filter and then pass the last model from the results into remove。
var matchingModels = this.where({type: 'foo'});
this.remove(matchingModels[matchingModels.length - 1]);