A
A
Armanio2014-03-14 00:38:47
JavaScript
Armanio, 2014-03-14 00:38:47

How to solve backbone collection filtering problem?

I decided that it was time to grow up and relearn how to code jquery - I chose backbone to start with, in order to more or less deal with client-side MVC.
The view is redrawn, the models are saved, but the collections are not filtered:
there is a collection with a filter (everything is simple):

var AdList = Backbone.Collection.extend({
  url: "/ads",
  model: app.Ad,
  byParam: function (param) {
    param = param || {};
    var filtered = this.filter(function (ad) {
      var byDevice = param.device ? _.contains(_.pluck(ad.get('devices'), 'title'), param.device) : 1,
        byPlatform = param.platform ? _.contains(_.pluck(ad.get('platforms'), 'title'), param.platform) : 1;
        var byOldVersions = 1, byLastVersion = 1;
        if(param.for_old_versions){
          byOldVersions = ad.get('for_old_versions');
        }
        if(param.for_last_version){
          byLastVersion = ad.get('for_last_version');
        }
      return byDevice && byPlatform && byOldVersions && byLastVersion;
    });
    // console.log('filtered', new AdList(filtered));
    return (filtered);
  },
});
app.Ads = new AdList([{"id":91,"title":"asldknnsadl"},  ....]); // тут json с сервера

Further in the view, we change all the values ​​​​of the inputs and the filter event fires, which is processed in this way:
this.on('filter', function(param){
    this.collection.reset(this.collection.filtering(param));
  }, this);

I replace the collection with new values ​​- everything is ok.
Render has a handler that redraws the list.
The essence of the problem is this: when
replacing the collection via reset, app.Ads drops for some reason - is this normal behavior?
If "yes", how to filter the collections correctly?
There is an idea to duplicate the collection inside and render a new filtered one, but it seems to me that this is some kind of crutch.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mike Grigorieff, 2014-03-14
@Armanio

reset - resets the entire collection and adds new models specified by you to it, i.e. judging by your code, you filtered the models and added them to the collection, models that do not satisfy the filter will drop.
That is, if you want to reset rerendering, then you need a new collection, or you can write a method in the view that will draw your collection for the specified filters:

this.on('filter', function(attr,val){
    $('collectionContainer').empty();
     _.each(this.collection.search(attr,val),this.addOne,this);
}

In the collection:
search: function(attribute,value) {
            var val = _.trim(value);
            
             if( val === '' ) {
                return this.models;
            }

            return this.filter(function(x){
                var regForCompany = new RegExp(val, "i");
                return x.get(attribute).match(regForCompany);
            });
        },

Well, the current search method should be rewritten under your conditions, otherwise I have conditions here for one property of the collection.

P
personaljs, 2014-04-04
@personaljs

In order to filter models in a collection, there are standard backbone functions this.collection.where({}) and this.collection.findWhere({}). Basically, they should be enough for you. Read more here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question