AmpersandJS / ampersand-subcollection

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

ampersand-subcollection

notice

This module is deprecated and further non-security updates will not be done on it. The functionality here has been broken into two modules:

ampersand-filtered-subcollection ampersand-paginated-subcollection

Please use them instead for new projects, and consider moving to one or both of them in your existing ones.

overview

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 collections, but really is a subset.

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

browser support

browser support

install

npm install ampersand-subcollection

example

var WidgetCollection = require('./mycollection');
var SubCollection = require('ampersand-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 SubCollection(widgets, {
    where: {
        awesome: true
    },
    comparator: function (model) {
        return model.rating;
    }
});

API reference

new SubCollection(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, watches, limit, and offset. 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.

.filtered

The array of filtered models before offset and/or limit is applied.

.models

The array of filtered models with offset and/or limit applied (if set).

all the underscore methods

Since we're already depending on underscore for much of the functionality in this module, we also mixin underscore methods into the subcollection in the same way that Backbone does for collections.

This means you can just call collection.each() or collection.find() to find/filter/iterate the models in the subcollection. You can see which underscore methods are included by referencing ampersand-collection-underscore-mixin.

SubCollection.extend(mixins...)

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

var SubCollection = require('ampersand-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 = SubCollection.extend({
    myMethod: function () { ... },
    myOtherMethod: function () { ... } 
});

This is done by using: ampersand-class-extend

changelog

credits

If you like this follow @HenrikJoreteg on twitter.

license

MIT