0
votes

I have a collection A and a view A

 collection A:
      model {type: A}
      model {type: B}

view A use collection A and all its models. If I add a new model to collection A, this model is added to view A.

In view B I want to use collection A as well, but not all its models, only models with type B. But I want to use all listeners in View A.

So, If I add a new model with type B in view A, listeners in view B should intercept this and add it to view B.

I can make two different collections, and use two listeners. But this looks dirty.

Is there a way to get get a selection of a collection into a new collection, but keep the same listeners and backbone functions as if it was just a reference to the main collection?

1

1 Answers

1
votes

One solution would be to have one collection replicate another (applying a filter at the same time).

From https://jsfiddle.net/t8e6Ldue/

var collectionB = new FilteredCollection(null, {
  source: collectionA,
  filter: function(model) {
    return model.get('type') === 'B';
  }
});

see the JSFiddle for full code