Fork me on GitHub

AFrameJS

- Javascript MVC Library

class AFrame.AObject

The base object of nearly everything. It is recommended to create all new classes as a subclass of AObject since it provides general functionality such as event binding and teardown housekeeping. All AObjects in the system have a cid, a cid is a unique identifier within the application. If an AObject creates and is responsible for maintaining other AObjects, addChild should be called with the created children. When this object is torn down, the child object added via addChild will have its teardown function called as well. This can ensure that all memory is freed and that no references are kept when the object's lifespan has ended.

Declaring Configuration Items to Import

A very common pattern used in AFrame derived objects is to save off a list of configuration options that an object is created with. Each class can define a list of configuration options that should automatically be imported on object creation.

Example Auto-Import of Configuration Items

// Define a class with items to import
var SomeClass = AFrame.Class( {
    importconfig: [ 'firstImportedParam', 'secondImportedParam' ]
} );

var someClassInst = SomeClass.create( {
    firstImportedParam: "This is imported",
    secondImportedParam: "So is this",
    thirdParam: "But this is not"
} );

Event Usage

All AFrame.AObject based classes have a built in event mechanism. Events are dynamically created, there is no need to explicitly create an event, all that is needed is to call the object's triggerEvent or bindEvent.

Event Example Usage:

// Assume anObject is an AFrame.AObject based object.
// Every AFrame.AObject based object triggers an onInit event
// when its init function is called.
var onObjectInit = function() {
   // called whenever anObject.init is called.
};

anObject.bindEvent( 'onInit', onObjectInit );
anObject.init();    // calls onObjectInit function

Declaring Event Bindings

Binding to dependent object's events is another common pattern used in AFrame. To make this process simpler, it is possible to declare event bindings. When declaring event bindings, three pieces of information are needed, the event name, the name of the object that is triggering the event, and the function (or name of the member function to bind as a handler.

Example Usage:

// bind to two events on insertedObj, event1, and event2.
// event1 has an inline handler.
// event2 uses a class member as a handler.
var Class = AFrame.AObject.extend( {
    importconfig: [ 'insertedObj' ],
    events: {
        'event1 insertedObj': function() {
            // Handle event here
        },
        'event2 insertedObj': 'event2Handler'
    },
    event2Handler: function() {
         // handle event here
    }
} );

Methods

Methods Returns Description
addChild( child ) void

Add a child. All children are torn down on this object's teardown

 // childToBeTornDown is an AObject based item created by this AObject.
 // childToBeTornDown needs torn down whenever this object is torn down.
 this.addChild( childToBeTornDown );

Parameters:

  • child <AObject>
    • child object
bindEvents( ) void

Override to do any event binding

getCID( ) void

Get the CID of the object

 var cid = this.getCID();
getConfig( ) object

Return the configuration object given in init.

 var config = this.getConfig();

Returns:

  • <object>

    the configuration object

init( config ) void

Initialize the object. Note that if a class' create static function is used to create an object, this will be called automatically.

var obj = new AFrame.SomeObject();
obj.init( { name: 'value' } );

Parameters:

  • config <object>
    • configuration
  • config.cid <id>
    • cid to give to the object, if not given, one is generated.
removeChild( cid ) void

Remove a child.

// childToRemove is a child that this object has already
// created and no longer needs.
this.removeChild( childToRemove.getCID() );

Parameters:

  • cid <cid>
    • cid of item to remove
teardown( ) void

Tear the object down, free any references

this.teardown();
triggerProxy( eventName ) void

Create a trigger event proxy function. Useful to re-broadcast an event or when a DOM event should trigger a normal AObject based event.

// use triggerProxy to rebroadcast an event from a child
child.bindEvent( 'eventToProxy', this.triggerProxy( 'eventToProxy' ) );

Parameters:

  • eventName <string>
    • name of event to trigger

Events

Events Description
onInit( event )

Triggered when the object is initialized

Parameters:

  • event <AFrame.Event>
    • the event object
onTeardown( event )

triggered whenever tte object is torn down

Parameters:

  • event <AFrame.Event>
    • the event

Configuration Attributes

Attributes Type Description
{cid} cid object

cid for the object, if not given, a unique id is assigned