Backbone GroupedCollection is for creating dynamic data strcutures like this.
In which you have a collection and you want to group the models in the collection into an arbitrary number of groups, based on attributes of the models.
Using this library allows you to do some things that could get quite complicated if not, for example:
- Sorting groups and models in groups seperately
- Appending new models to the right group automatically
- Adding and removing groups when models are added or removed
- Creating a dynamic tree that responds to change events in the models
How does it work?
Backbone.buildGroupedCollection recieves a base collection and a
groupBy function. It returns a collection of models (groups).
Each of the groups has it's group identifier as the id, and a special property
vc which is a Backbone.VirtualCollection of the models that belong in the group.
It works well with Marionette or stand-alone.
var animals =name: 'cat' color: 'black'name: 'dog' color: 'black'name: 'bird' color: 'red';var grouped_animals = Backbone;
The structure looks something like this:
- Group (black)
- Animal (cat)
- Animal (dog)
- Group (red)
- Animal (bird)
- Group (black)
And the structure adapts itself constantly to changes in the base colleciton.
grouped_animals; // ['black', 'red']grouped_animalsvclength; // 2grouped_animalsvclength; // 1// adding elementsanimals;grouped_animals; // ['black', 'red', 'green']grouped_animalsvc === animals; // true// removing elementsanimals;grouped_animals; // ['black', 'green']
Somtimes you want to make sub-groups, or infinitely nested groups. For example, you have a shopping list that you want grouped by store, and then within each store, by item type.
var grouped_shopping_list = Backbone;// the items of type "office supplies" from the store "home depot" will be accesible atgrouped_shopping_listgrouped_vc // retutns a virtual collection
The base collection.
The function that defines the grouping. It recieves a model as the argument, and it must return the id of the group it belongs to.
The GroupCollection will continue listening to the base collection untill you call
stopListening() on it. You can bind the lifespan of a grouped_collection
to that of a marionette view by specifying a
close_with option. (it can also be any event emitter that emits a
Specifies an alternative 'class' for the GroupCollection to have. It should extend from Backbone.Collection.
Specifes an alternative 'class' for the Groups to have. It should extend from Backbone.Model;
Object passed to the virtual collections as options. Use this to configure comparators, etc.
0.1.3 Change package name to allow publishing on npm 0.1.2 Add vc_options via @masylum. Fix close_with handling 0.1.1 Add listeners for 'change', 'remove' and 'reset' only once 0.1.0 Fixes groups not being deleted because a race condition with `VirtualCollection` 0.0.3 Fixes bug when changing a model failed to create a new Group 0.0.2 Add GroupCollection & Group options
The MIT License (MIT)
Copyright (c) 2013 Pedro firstname.lastname@example.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.