https://github.com/dawee/modella-collector
modella plugin to enable collection attributes
https://github.com/dawee/modella-collector
Last synced: over 1 year ago
JSON representation
modella plugin to enable collection attributes
- Host: GitHub
- URL: https://github.com/dawee/modella-collector
- Owner: dawee
- Created: 2014-04-26T13:20:11.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-06-17T14:26:32.000Z (about 12 years ago)
- Last Synced: 2025-03-15T08:18:41.530Z (over 1 year ago)
- Language: JavaScript
- Size: 191 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# modella-collector
[modella](http://github.com/modella/modella) plugin to enable collection attributes.
## Declaration
```js
var modella = require('modella');
var Child = modella('Child').attr('name');
var Mom = modella('Mom').attr('children', {type: [Child]}).use(collector);
var mom = new Mom({children: [
{name: 'Pete'}, // Collection models can be initialized with data
new Child({name: 'jack'}) // or with instance
]});
```
_This plugin is inspired by [this discussion](https://github.com/modella/modella/issues/24)_
## Playing with Collections
### Collection#get(index)
Returns a model at a given index.
```js
mom.get('children').get(0).name(); // Pete
```
### Collection#first()
Returns the first model.
```js
mom.get('children').first().name(); // Pete
```
### Collection#last()
Returns the last model.
```js
mom.get('children').last().name(); // Jack
```
### Collection#add(model)
Adds a model
```js
mom.get('children').add({name: 'Mickael'}); // With data
mom.get('children').add(new Child({name: 'John'})); // With instance
```
### Collection#each(fn)
Iterates over the collection with the given function.
```js
mom.get('children').each(function (child) {
console.log(child.name());
});
```
### Collection#every(fn)
Iterates over the collection.
Returns true if every predicates called returns true.
```js
mom.get('children').every(function (child) {
return child.name().length > 3;
});
```
### Collection#some(fn)
Iterates over the collection.
Returns true if at least one predicate called returns true.
```js
mom.get('children').some(function (child) {
return child.name() === 'John';
});
```