exclude function for collections. #996

Closed
rmarinleal opened this Issue Mar 4, 2013 · 1 comment

Comments

Projects
None yet
2 participants
@rmarinleal

I came here redirected from a backbone issue:
documentcloud/backbone#2331

When I populate a collection with models by a rest request, I can apply a local where but only returns models than pass the where validation, but what I need is exactly the opposite, I need that the collection return me all models except the ones than apply the conditions.

I develope my own function, but I think is something very common, lets think on this "select query" example:

"select * from employees where company <> 'microsoft'"...and apply this to backbone collections.
I don't know wich companies I permit to the employees (because I don't know wich companies are in the database) buy what I know is than the can't work for Microsoft (very common ;) )

Thanks!

@rmarinleal rmarinleal referenced this issue in jashkenas/backbone Mar 4, 2013

Closed

collection.omit (or similar) #2331

@rmarinleal

This comment has been minimized.

Show comment Hide comment
@rmarinleal

rmarinleal Mar 4, 2013

this is my extended funtion added to backbone collections

    /**
     * This function returns the models of the collection wich pass the "where" filters, and over the result
     * apply an extra excusion filter with the params passed as "exclude".
     */
    filter: function(where, exclude) {
      var me = this; 
      // if we pass undefineds, we doesn's apply any filters
      if (!(typeof where !== "undefined" || typeof exclude !== "undefined")) 
      {
        return false;
      }
      // Search for the models that pass the where filters
      var moFiltered = (typeof where === "undefined") ? false : _.isEmpty(where) ? [] : this.filter(function(model) 
      {
        for (var key in where) 
        {
          if (where[key] !== model.get(key))
          {
            return false;
          }
        }
        return true;
      });
      
      // Search for the models that apply the exclude params
      var moFilteredExclude = _.isEmpty(exclude) ? [] : this.filter(function(model) {
      for (var keyExl in exclude) {
          if (exclude[keyExl] !== model.get(keyExl)) return false;
        }
        return true;
      });
      if (!_.isEmpty(moFilteredExclude))
      {
        // If does't pass where filters, and pass exclude filters, we work with all models.
        if (!_.isArray(moFiltered))
        {
          moFiltered = me.models;
        }
        var deletions = [];
        for (var existingModel in moFiltered)
        {
          for (var excludingModel in moFilteredExclude) 
          {
            if (_.isEqual(moFiltered[existingModel], moFilteredExclude[excludingModel]))
            {
              deletions.push(existingModel);
              moFiltered.splice(existingModel,1);
            }
          }
        }
      }
      return moFiltered;
    }

this is my extended funtion added to backbone collections

    /**
     * This function returns the models of the collection wich pass the "where" filters, and over the result
     * apply an extra excusion filter with the params passed as "exclude".
     */
    filter: function(where, exclude) {
      var me = this; 
      // if we pass undefineds, we doesn's apply any filters
      if (!(typeof where !== "undefined" || typeof exclude !== "undefined")) 
      {
        return false;
      }
      // Search for the models that pass the where filters
      var moFiltered = (typeof where === "undefined") ? false : _.isEmpty(where) ? [] : this.filter(function(model) 
      {
        for (var key in where) 
        {
          if (where[key] !== model.get(key))
          {
            return false;
          }
        }
        return true;
      });
      
      // Search for the models that apply the exclude params
      var moFilteredExclude = _.isEmpty(exclude) ? [] : this.filter(function(model) {
      for (var keyExl in exclude) {
          if (exclude[keyExl] !== model.get(keyExl)) return false;
        }
        return true;
      });
      if (!_.isEmpty(moFilteredExclude))
      {
        // If does't pass where filters, and pass exclude filters, we work with all models.
        if (!_.isArray(moFiltered))
        {
          moFiltered = me.models;
        }
        var deletions = [];
        for (var existingModel in moFiltered)
        {
          for (var excludingModel in moFilteredExclude) 
          {
            if (_.isEqual(moFiltered[existingModel], moFilteredExclude[excludingModel]))
            {
              deletions.push(existingModel);
              moFiltered.splice(existingModel,1);
            }
          }
        }
      }
      return moFiltered;
    }

@jashkenas jashkenas closed this Jul 6, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment