Andy
Andy

Reputation: 19251

Backbonejs Search Collection with Model with Object default

I have a model like this

var myModel = Backbone.Model.extend({
  defaults: {
      a: '',
      b: '',
      c: ''
  }
});

My collection myCollection gets populated with the data and the c value is a list of objects.

I am trying to figure out how I can lookup a value within myCollection such that it matches the items within my models c and returns that model if a match is found ?

The values in c are objects like

"c": {"1": {}, "2": {}, "3": {}} 

Upvotes: 1

Views: 86

Answers (1)

Paige Ruten
Paige Ruten

Reputation: 176675

Use the find() function, which iterates over the collection and returns the first model that matches your conditions. Something like this:

result = myCollection.find(function (model) {
  return model.get("c").indexOf(5) != -1;
});

Or use filter() instead of find(), to get all of the models that pass the condition.

Upvotes: 1

Related Questions