Fork me on GitHub

AFrameJS

- Javascript MVC Library

class AFrame.CollectionArray class extends AFrame.CollectionHash

An array collection. Unlike the CollectionHash, the CollectionArray can be accessed via either a key or an index. When accessed via a key, the item's CID will be used. If an item has a cid field when inserted, this cid will be used, otherwise a cid will be assigned.

This raises the same events as AFrame.CollectionHash, but every event will have one additional parameter, index.

Create the array
var collection = AFrame.CollectionArray.create();

// First item is inserted with a cid, inserted at the end of the array.
var aframeCID = collection.insert( { cid: 'cid1',
                         name: 'AFrame Foundary',
                         city: 'London',
                         country: 'United Kingdom'
                       } );
// aframeCID variable will be 'cid1'

// inserts google at the head of the list.
var googleCID = collection.insert( { name: 'Google',
                               city: 'Santa Clara',
                               country: 'United States'
                             }, 0 );
// googleCID will be assigned by the system

// microsoft inserted at the end of the list.
var microsoftCID = collection.insert( { name: 'Microsoft',
                               city: 'Redmond',
                               country: 'United States'
                             }, -1 );
// microsoftCID will be assigned by the system

// Getting an item via index.  This will return google item.
var item = collection.get( 0 );
// item will be the google item

// Getting an item via negative index.  This will return microsoft item.
var item = collection.get( -1 );
// item will be the microsoft item

// Getting an item via CID.  This will return the aframe item.
item = collection.get( aframeCID );

var googleItem = collection.remove( googleCID );
// googleItem will be the google item that was inserted

var aframeItem = collection.remove( 0 );
// aframeItem will be the aframe item since the google item was first but is now removed

Constructor

Constructor Parameters Returns
AFrame.CollectionArray( )

Methods

Methods Returns Description
clear( ) void

Clear the array

// Clears the collection.
collection.clear();
get( index ) variant

Get an item from the array.

// Getting an item via index.  This will return google item.
var item = collection.get( 0 );
// item will be the google item

// Getting an item via negative index.  This will return microsoft item.
var item = collection.get( -1 );
// item will be the microsoft item

// Getting an item via CID.  This will return the aframe item.
item = collection.get( aframeCID );

Parameters:

  • index <number || id>
    • index or cid of item to get
    • Returns:

      • <variant>

        item if it exists, undefined otw.

getArray( ) array

Get an array representation of the CollectionArray

// Returns the array representation of the collection.
var itemsArray = collection.getArray();

Returns:

  • <array>

    array representation of CollectionArray

getCID( index ) void private

Given an index or cid, get the cid.

Parameters:

  • index <id || number>
getCount( ) number

Get the current count of items

// Get the number of items in the collection.
var count = collection.clear();

Returns:

  • <number>

    current count

getEventObject( ) void private
getIndex( index ) void private

Given an index or cid, get the index.

Parameters:

  • index <id || number>
insert( item, index ) id

Insert an item into the array.

// First item is inserted with a cid, inserted at the end of the array.
var aframeCID = collection.insert( { cid: 'cid1',
                         name: 'AFrame Foundary',
                         city: 'London',
                         country: 'United Kingdom'
                       } );
// aframeCID variable will be 'cid1'

// inserts google at the head of the list.
var googleCID = collection.insert( { name: 'Google',
                               city: 'Santa Clara',
                               country: 'United States'
                             }, 0 );
// googleCID will be assigned by the system

// microsoft inserted at the end of the list.
var microsoftCID = collection.insert( { name: 'Microsoft',
                               city: 'Redmond',
                               country: 'United States'
                             }, -1 );
// microsoftCID will be assigned by the system

Parameters:

  • item <variant>

    to insert

  • index <integer>

    (optional) - index to insert into. If not defined, insert at the end of the list.

    • Returns:

      • <id>

        cid of the item

remove( index ) void

Remove an item from the array

var googleItem = collection.remove( googleCID );
// googleItem will be the google item that was inserted

var aframeItem = collection.remove( 0 );
// aframeItem will be the aframe item since the google item was first but is now removed

Parameters:

  • index <number || id>

    of item to remove.

Events inherited from AFrame.AObject

Events Notes
onInit
onTeardown

Events inherited from AFrame.CollectionHash

Events Notes
onBeforeInsert
onBeforeRemove
onInsert
onRemove

Configuration Attributes inherited from AFrame.AObject

Attributes Notes
{cid} cid