Fork me on GitHub

AFrameJS

- Javascript MVC Library

class AFrame.CollectionHash class extends AFrame.AObject

A hash collection. Items stored in the hash can be accessed/removed by a key. The item's key is first searched for on the item's cid field, if the item has no cid field, a cid will be assigned to it. The CID used is returned from the insert function.

CollectionHash is different from a CollectionArray which is accessed by index.

Create the hash
var collection = AFrame.CollectionHash.create();

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

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

// Getting an item from the hash.
var item = collection.get( 'cid1' );
// item will be the AFrame Foundary item

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

Known subclasses

Constructor

Constructor Parameters Returns
AFrame.CollectionHash( )

Methods

Methods Returns Description
clear( ) void

Clear the hash

// remove all items from the hash.
hash.clear();
forEach( callback, context ) void

Iterate over the collection, calling a function once for each item in the collection.

// iterate over the collection
collection.forEach( function( item, id ) {
    // perform some action on item.
} );

Parameters:

  • callback <function>
    • callback to call for each item. Will be called with two parameters, the first is the item, the second the identifier (id type depends on type of collection).
  • context <object>
    • optional context to call callback in.
get( cid ) variant

Get an item from the hash.

// using data from example at top of page
var item = hash.get( 'cid1' );
// item will be the AFrame Foundary item

Parameters:

  • cid <id>
    • cid of item to get
    • Returns:

      • <variant>

        item if it exists, undefined otw.

insert( item, options ) id

Insert an item into the hash. CID is gotten first from the item's cid field. If this doesn't exist, it is then assigned. Items with duplicate cids are not allowed, this will cause a 'duplicate cid' exception to be thrown. If the item being inserted is an Object and does not already have a cid, the item's cid will be placed on the object under the cid field.

When onBeforeInsert is triggered, if the event has had preventDefault called, the insert will be cancelled

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

var googleCID = hash.insert( { name: 'Google',
                               city: 'Santa Clara',
                               country: 'United States'
                             } );
// googleCID will be assigned by the system

Parameters:

  • item <variant>
    • item to insert
  • options <object>
    • options
  • options.force <boolean>
    • force insertion, if set to true, onBeforeInsert event has no effect (duplicate cid constraints still apply)
    • Returns:

      • <id>

        cid of the item.

remove( item, options ) variant

Remove an item from the store.

// using data from example at top of page, remove by CID
var googleItem = hash.remove( googleCID );
// googleItem will be the google item that was inserted

// remove by item itself
hash.remove( googleItem );

An Example can be found on JSFiddle

Parameters:

  • item <object || cid>
    • item or cid of item to remove
  • options <object>
    • options
  • options.force <boolean>
    • force removal, if set to true, onBeforeRemove event has no effect.
    • Returns:

      • <variant>

        item if it exists, undefined otw.

Events

Events Description
onBeforeInsert( data )

Triggered before insertion happens. If listeners call preventDefault on the event, item will not be inserted

Parameters:

  • data <object>
    • data has two fields.
  • data.collection <CollectionHash>
    • collection causing event.
  • data.item <variant>
    • item inserted
onBeforeRemove( data )

Triggered before remove happens. If listeners call preventDefault on the event object, the remove will not happen.

Parameters:

  • data <object>
    • data field passed.
  • data.collection <CollectionHash>
    • collection causing event.
  • data.item <variant>
    • item removed
onInsert( data )

Triggered after insertion happens.

Parameters:

  • data <object>
    • data has two fields, item and meta.
  • data.collection <CollectionHash>
    • collection causing event.
  • data.item <variant>
    • item inserted
onRemove( data )

Triggered after remove happens.

Parameters:

  • data <object>
    • data has two fields, item and meta.
  • data.collection <CollectionHash>
    • collection causing event.
  • data.item <variant>
    • item removed

Events inherited from AFrame.AObject

Events Notes
onInit
onTeardown

Configuration Attributes inherited from AFrame.AObject

Attributes Notes
{cid} cid