AmpersandJS / ampersand-filtered-subcollection

Filterable, sortable, proxy of a collection that behaves like a collection.
MIT License
11 stars 7 forks source link

ampersand-filtered-subcollection

Lead Maintainer: Michael Garvin

Purpose

Filtered subset of a collection that emits events like a collection.

Often for one part of an app you want a whole collection of models, but for another you want some sort of filtered subcollection. That's what this is for. It gives you a "pseudo collection" that behaves much like a full collection, but really is a subset.

Part of the Ampersand.js toolkit for building clientside applications.

browser support

browser support

install

npm install ampersand-filtered-subcollection

example

var WidgetCollection = require('./mycollection');
var FilteredSubcollection = require('ampersand-filtered-subcollection');

var widgets = new WidgetCollection();

widgets.fetch();

// this will create a collection-like object
// that will only include models that match
// the `where` filters.
// It will be sorted by the comparator
// independent of base collection order
var favoriteWidgets = new FilteredSubcollection(widgets, {
    where: {
        awesome: true
    },
    comparator: function (model) {
        return model.rating;
    }
});

API reference

new FilteredSubcollection(collection, [config])

.configure(config, [reset])

Config can get used to update subcollection config post-init.

.reset()

Convenience method that calls .configure({}, true)

.addFilter(filterFunction)

.removeFilter(filterFunction)

.clearFilters()

Removes filter functions and watches. After calling this, the subcollection should have the same models as your base collection.

The only thing that does not get cleared is your comparator method or property if you have one.

.swapFilters(newFilters, [oldFilters])

Replaces a set of existing filter functions with a set of new filters, and does not apply the results of the new filter combination until all have been added and removed.

For example:

.swapFilters(newFilter, []) // Same as .addFilter(newFilter)
.swapFilters([], oldFilter) // Same as .removeFilter(oldFilter)

.at(index)

.length

The subcollection maintains a read-only length property that simply proxies to the array length of the models it contains.

.models

The array of filtered models

FilteredSubcollection.extend(mixins...)

FilteredSubcollection attaches extend to the constructor so if you want to add custom methods to your subcollection constructor, it's easy:

var FilteredSubcollection = require('ampersand-filtered-subcollection');

// this exports a new constructor that includes
// the methods you passed on the prototype while
// maintaining the inheritance chain for instanceof
// checks.
module.exports = FilteredSubcollection.extend({
    myMethod: function () { ... },
    myOtherMethod: function () { ... }
});

This is done by using: ampersand-class-extend

credits

If you like this follow @HenrikJoreteg on twitter.

license

MIT