Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/agonbina/modella-collection
[Deprecated] A Collection plugin for modella.
https://github.com/agonbina/modella-collection
Last synced: about 1 month ago
JSON representation
[Deprecated] A Collection plugin for modella.
- Host: GitHub
- URL: https://github.com/agonbina/modella-collection
- Owner: agonbina
- Created: 2014-09-06T14:26:58.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-11-21T02:58:03.000Z (about 10 years ago)
- Last Synced: 2024-04-14T20:06:25.593Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 242 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
You should not use this as its no longer supported
===[ ![Codeship Status for agonbina/modella-collection](https://www.codeship.io/projects/0688df10-180b-0132-fe58-066ba6aa23b2/status)](https://www.codeship.io/projects/34189)
# modella-collection
A Collection plugin for [modella](https://github.com/modella/modella).## Installation
With node.js:
npm install modella-collection
In the browser (using [component](https://github.com/component/component)):
component install agonbina/modella-collection
## Example
```
var Animal = modella('Animal').attr('id').attr('type', { type: 'string' });
var User = modella('User')
.attr('id')
.attr('pets', { type: [ Animal ] })
.use(collection);var cat = new Animal({ id: 'kitty', type: 'cat' });
var me = new User({ id: 'agonbina', pets: [ cat ] });me.pets().each(function(pet) {
console.log(pet.id()); // 'kitty'
});var pets = me.get('pets');
```
For more examples see [examples](/example).## API
Every Collection instance will inherit all [Enumerable](https://github.com/component/enumerable) methods, so go check them out!
Below are some of the methods that you would expect to have in a collection.### Collection#.add(instance:Model|obj:Mixed)
Add a new instance to the collection. Emits ```'add'``` event.
```
pets.on('add', function(newPet) { });pets.add({ id: 'doggy', type: 'dog'});
pets.last() instanceof Animal ==> trueor
var newCat = new Animal({ id: 'catze', type: 'cat' });
pets.add(newCat);
```### Collection#.remove(instance:Model)
Remove an instance from the collection. Emits ```'remove'``` event.
```
pets.on('remove', function(removedPet) { });pets.remove(newCat);
pets.has(newCat); // false
```### Collection#.removeWhere(query:Mixed)
Remove all instances matching the given query.
```
pets.removeWhere({ type: 'cat' })var cats = pets.select(function(pet) { return pet.type() === 'cat'; })
cat.length(); // 0
```### Collection#.reset()
Reset the collection to an empty array.### Collection#.toJSON()
Recursively travel through all the models and call their .toJSON methods to compile an array with all of them.## Roadmap
- Add docs about all the whole available API
- Implement event propagation from collections to their parent(model)