modella plugin to enable collection attributes.
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
Returns a model at a given index.
mom.get('children').get(0).name(); // Pete
Returns the first model.
mom.get('children').first().name(); // Pete
Returns the last model.
mom.get('children').last().name(); // Jack
Adds a model
mom.get('children').add({name: 'Mickael'}); // With data
mom.get('children').add(new Child({name: 'John'})); // With instance
Iterates over the collection with the given function.
mom.get('children').each(function (child) {
console.log(child.name());
});
Iterates over the collection. Returns true if every predicates called returns true.
mom.get('children').every(function (child) {
return child.name().length > 3;
});
Iterates over the collection. Returns true if at least one predicate called returns true.
mom.get('children').some(function (child) {
return child.name() === 'John';
});