1

I have a Backbone.Pageable collection on which I am trying to do a filter but and reset the collection with the filtered values but after the reset, collection.fullCollection has one less model than the original.

This is my collection:

var todoCollection = Backbone.PageableCollection.extend({
  mode:'client',
  search: function(letters){
    var self = this;
    if(letters === "") return this.fullCollection.models;

    var pattern = new RegExp(letters,"i");
    return this.fullCollection.filter(function(data) {
      return pattern.test(data.get("text"));
    });
  }
});

You can check at this fiddle here.

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
shahsank3t
  • 252
  • 1
  • 13
  • The backbone collection seems to be working as expected. It's the plugins `fullCollection` thingy that isn't getting updated. Do they claim to support such feature? If so maybe an this is an issue with the paginator plugin..? Did you try raising an issue in github..? – T J Apr 09 '16 at 17:21
  • I am not sure if they claim such a feature but I have seen it's usage in this particular fashion and working fine. I'll try giving it one more good try before raising an issue in github. – shahsank3t Apr 10 '16 at 07:20

1 Answers1

0

Your search function should return an instance of todoCollection.

var todoCollection = Backbone.PageableCollection.extend({

mode:'client',
  search: function(letters){
    var self = this;
    if(letters === "") return this.fullCollection.models;

    var pattern = new RegExp(letters,"i");
    result =  this.fullCollection.filter(function(data) {
      return pattern.test(data.get("text"));
    });
    return new todoCollection(result);
  }

Working fiddle

Abhinav
  • 622
  • 6
  • 15
  • My issue isn't with returning of models / instance of todoCollection but the reset of todoCollection slicing my first model from todoCollection.fullCollection which it shouldn't do. (fullCollection is a part of paginator plugin) – shahsank3t Apr 10 '16 at 07:22
  • Can have a look at the updated fiddle link in my post? it is no longer slicing the first model. Although I don't know why it was doing it in the first place. – Abhinav Apr 10 '16 at 07:50
  • I looked at the updated fiddle and it shows correct length but the filtered model in the newly reset collection is not the right one. You can console.log the reset collection and check it's model to see what I am trying to say. – shahsank3t Apr 10 '16 at 08:10