Backbone collection find I want only one result

2019-09-19 13:31发布

问题:

I'm working with find Backbone and I have 20-30 results, I need the first result and I don't have the id for filter for id.

model = coleccion.find(
    function(e){
        return (
           e.get('car').category === data_category &&
           e.get('full_empty') === 0 &&
           e.get('idprovider') === data_provider
        );
    }
);

回答1:

You can use collection.at(0) if you want to get the first model. Or collection.shift() if you want to get the first model, then remove this model from the collection. Read more at Backbonejs Collection



回答2:

You can use findWhere method:

collection.findWhere(attributes)