UNPKG

6.11 kBMarkdownView Raw
1# ampersand-subcollection
2
3Filtered subset of a collection that emits events like a collection.
4
5Often for one part of an app you want a whole collection of models, but for another you want some sort of filterd 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.
6
7<!-- starthide -->
8Part of the [Ampersand.js toolkit](http://ampersandjs.com) for building clientside applications.
9<!-- endhide -->
10
11## browser support
12
13[![browser support](https://ci.testling.com/ampersandjs/ampersand-subcollection.png)
14](https://ci.testling.com/ampersandjs/ampersand-subcollection)
15
16## install
17
18```
19npm install ampersand-subcollection
20```
21
22## example
23
24```javascript
25var WidgetCollection = require('./mycollection');
26var SubCollection = require('ampersand-subcollection');
27
28
29var widgets = new WidgetCollection();
30
31widgets.fetch();
32
33// this will create a collection-like object
34// that will only include models that match
35// the `where` filters.
36// It will be sorted by the comparator
37// independent of base collection order
38var favoriteWidgets = new SubCollection(widgets, {
39 where: {
40 awesome: true
41 },
42 comparator: function (model) {
43 return model.rating;
44 }
45});
46```
47
48## API reference
49
50### new SubCollection(collection, [config])
51
52* `collection` {Collection} An instance of an ampersand-collection or Backbone.Collection that contains our full set of models.
53* `config` {Object} [optional] The config object that specifies whether or not a model in the base should be considered part of this subcollection.
54 * `where` {Object} [optional] Object where each key is a property name of the model and the value is what you want that property to be in order for it to be included. Often used for boolean properties.
55 * `filter` {Function} [optional] If you need more control than what you get from `where` you can use a filter function to determine if the model should be included. It will get called with a model and you simply return `true` or `false`.
56 * `filters` {Array} [optional] If you for some reason want to pass in multiple filter functions you can do so. This can be useful in cases where you keep a reference to one that you may remove later without wanting to remove all your filtering rules. But, most of the time you would just do use `filter` and do all your logic in that one function.
57 * `watched` {Array} [optional] This is an array of property names to watch for changes to in the base collection. This happens automatically if you use `where`
58 * `comparator` {Function || String} [optional] If you want to determine sort order separate from the base collection provide this argument. If you pass a string it should be the name of the property that should be used to sort by. If you pass a function, it will be passed the model and should return the value from the model that should be used to sort. If you pass a function that names two incoming arguments it will be used as a native Array.prototype.sort, where you get passed two models and return a 1, 0, -1 to specify how they compare.
59 * `limit` {Number} [optional] If specified will limit the number of matched models to this maximum number. This is useful for things like pagination.
60 * `offset` {Number} [optional] Similar to `limit` setting an `offset` will specify what number to start at. So you can think of `limit` as number of results per page, and `offset` being the index of the start of the current page of results.
61
62### .configure(config, [reset])
63
64Config can get used to update subcollection config post-init.
65
66* `config` {Object} Same config object as what you pass to init.
67* `reset` {Boolean} Default: false. Whether or not to remove all previous filter config options. If you specify `{where: {read: true}}` in the init and then do `.configure({where: {from: 'steve'}})` without passing `true` the collection will contain only read items from steve. They filters are combined by default.
68
69### .addFilter(filterFunction)
70
71* `filterFunction` {Function} A filter function as described above. Gets called with the model, you return `true` or `false`.
72
73### .removeFilter(filterFunction)
74
75* `filterFunction` {Function} If you have a reference in your code to the filter function you added, you can remove it by calling `removeFilter`.
76
77### .clearFilters()
78
79Removes all filter functions and resets everything. After calling this, the subcollection should have the same models as your base collection.
80
81The only thing that does *not* get cleared is your `comparator` method if you have one.
82
83### .at(index)
84
85* `index` {Number} returns model as specified index in the subcollection.
86
87### .length
88
89The subcollection maintains a read-only length property that simply proxies to the array length of the models it contains.
90
91### all the underscore methods
92
93Since 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.
94
95This 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](https://github.com/AmpersandJS/ampersand-collection-underscore-mixin).
96
97### SubCollection.extend(mixins...)
98
99Subcollection attaches `extend` to the constructor so if you want to add custom methods to your subcollection constructor, it's easy:
100
101```javascript
102var SubCollection = require('ampersand-subcollection');
103
104// this exports a new constructor that includes
105// the methods you passed on the prototype while
106// maintaining the inheritance chain for instanceof
107// checks.
108module.exports = SubCollection.extend({
109 myMethod: function () { ... },
110 myOtherMethod: function () { ... }
111});
112```
113
114This is done by using: [ampersand-class-extend](https://github.com/AmpersandJS/ampersand-class-extend)
115
116
117## credits
118
119If you like this follow [@HenrikJoreteg](http://twitter.com/henrikjoreteg) on twitter.
120
121## license
122
123MIT
124