To create an edb.Array constructor, simply extend from edb.Array.

var Persons = edb.Array.extend({
	$of: Person

The magic property $of instructs the edb.Array to instantiate a Person for every JSON object is added, so let's try that out.

var persons = new Persons({
	firstname: 'Bob',
	lastname: 'Schmidt' 
console.log(persons[0].fullname()); // Bob Schmidt
persons.push({ firstname: 'Jim', lastname: 'Nielsen' });
console.log(persons[1].fullname()); // Jim Nielsen
persons.unshift({ firstname: 'Ole', lastname: 'Jensen' });
console.log(persons[0].fullname()); // Ole Jensen

If an already instantiated object other than a Person is added, the Array will throw an error. Let's also try that out.

persons.push(new Animal()); // TypeError

Dynamic $of

Hej hej.

Arrays versus Objects

You can add methods to the Array, much like an edb.Object.

Persons = edb.Array.extend({
	$of: Person,
	lookup: function(name) {
		return this.filter(function(person) {
			return person.fullname() === name;
		})[0] || null;

You can also add properties.

Persons = edb.Array.extend({
	$of: Person,
	lucky_number: 23

Unline an edb.Object, the properties of an Array cannot be set via constructor arguments. You'll need to set them afterwards, like this.

var persons = new Persons(json1, json2, json3);
persons.lucky_number = 42;

Populating Arrays

When you populate a edb.Array, you can do so with an array argument if that is more convenient. For real data, it usually is.

var persons = new Persons([ // notice square brackets
	{ firstname: 'Bob', lastname: 'Schmidt' },
	{ firstname: 'Ole', lastname: 'Honnoré' }

When you do this, remember that the Array must be declared $of a type edb.Object, because the example code will otherwise push a (nested) edb.Array into the list.

Anonymous Arrays