# Yahoo! UI Library

### base 3.0.0

Yahoo! UI Library > base > Base
Search:
Filters
Show Private Show Protected Show Deprecated

## Class Base - uses Attribute , Plugin.Host

Known Subclasses:
  Plugin.DragConstrained DataSource.Local Widget Plugin.Base DD.Drop ImgLoadGroup Plugin.NodeFX ImgLoadImgObj DD.Drag DD.Scroll Anim Plugin.DDProxy DD.DDM

A base class which objects requiring attributes and custom event support can extend. Base also handles the chaining of initializer and destructor methods across the hierarchy as part of object construction and destruction. Additionally, attributes configured through the static ATTRS property for each class in the hierarchy will be initialized by Base.

The static NAME property of each class extending from Base will be used as the identifier for the class, and is used by Base to prefix all events fired by instances of that class.

### Constructor

Base `( config )`

Parameters:
  `config <Object>` Object with configuration property name/value pairs

### Properties

#### Base._ATTR_CFG - `private static Array`

The list of properties which can be configured for each attribute (e.g. setter, getter, writeOnce, readOnly etc.)

#### Base._buildCfg - `private static final Object`

The build configuration for the Base class. Defines the static fields which need to be aggregated when the Base class is used as the main class passed to the Base.build method.

#### Base.ATTRS - `static Object`

The default set of attributes which will be available for instances of this class, and their configuration. In addition to the configuration properties listed by Attribute's addAttr method, the attribute can also be configured with a "cloneDefaultValue" property, which defines how the statically defined value field should be protected ("shallow", "deep" and false are supported values). By default if the value is an object literal or an array it will be "shallow" cloned, to protect the default value.

#### Base.NAME - `static String`

The string to be used to identify instances of this class, for example in prefixing events.

Classes extending Base, should define their own static NAME property, which should be camelCase by convention (e.g. MyClass.NAME = "myClass";).

#### name - `String`

The string used to identify the class of this object.
Deprecated: Use this.constructor.NAME

#### Properties inherited from Attribute:

``

### Methods

#### _aggregateAttrs

`private Object _aggregateAttrs ( allAttrs )`
A helper method, used by _initHierarchyData to aggregate attribute configuration across the instances class hierarchy. The method will potect the attribute configuration value to protect the statically defined default value in ATTRS if required (if the value is an object literal, array or the attribute configuration has cloneDefaultValue set to shallow or deep).

Parameters:
  `allAttrs <Array>` An array of ATTRS definitions across classes in the hierarchy (subclass first, Base last)

Returns: `Object`
  The aggregate set of ATTRS definitions for the instance

#### _defDestroyFn

`protected void _defDestroyFn ( e )`
Default destroy event handler

Parameters:
  `e <EventFacade>` Event object

#### _defInitFn

`protected void _defInitFn ( e )`
Default init event handler

Parameters:
  `e <EventFacade>` Event object, with a cfg property which refers to the configuration object passed to the constructor.

#### _destroyHierarchy

`private void _destroyHierarchy ( )`
Destroys the class hierarchy for this instance by invoking the descructor method on the prototype of each class in the hierarchy.

#### _filterAttrCfs

`private Object _filterAttrCfs ( clazz , allCfgs )`
A helper method used when processing ATTRS across the class hierarchy during initialization. Returns a disposable object with the attributes defined for the provided class, extracted from the set of all attributes passed in .

Parameters:
  `clazz <Function>` The class for which the desired attributes are required.
  `allCfgs <Object>` The set of all attribute configurations for this instance. Attributes will be removed from this set, if they belong to the filtered class, so that by the time all classes are processed, allCfgs will be empty.

Returns: `Object`
  The set of attributes belonging to the class passed in, in the form of an object with attribute name/configuration pairs.

#### _getAttrCfgs

`protected Object _getAttrCfgs ( )`
Returns an aggregated set of attribute configurations, by traversing the class hierarchy.

Returns: `Object`
  The hash of attribute configurations, aggregated across classes in the hierarchy This value is cached the first time the method, or _getClasses, is invoked. Subsequent invocations return the cached value.

#### _getClasses

`protected Function[] _getClasses ( )`
Returns the class hierarchy for this object, with Base being the last class in the array.

Returns: `Function[]`
  An array of classes (constructor functions), making up the class hierarchy for this object. This value is cached the first time the method, or _getAttrCfgs, is invoked. Subsequent invocations return the cached value.

#### _initHierarchy

`private void _initHierarchy ( userVals )`
Initializes the class hierarchy for the instance, which includes initializing attributes for each class defined in the class's static ATTRS property and invoking the initializer method on the prototype of each class in the hierarchy.

Parameters:
  `userVals <Object>` Object with configuration property name/value pairs

#### _initHierarchyData

`private void _initHierarchyData ( )`
A helper method used by _getClasses and _getAttrCfgs, which determines both the array of classes and aggregate set of attribute configurations across the class hierarchy for the instance.

#### Base.build

`static Function Base.build ( name , main , extensions , cfg )`

Builds a custom constructor function (class) from the main function, and array of extension functions (classes) provided. The NAME field for the constructor function is defined by the first argument passed in.

The cfg object supports the following properties

dynamic <boolean>

  If true (default), a completely new class is created which extends the main class, and acts as the host on which the extension classes are augmented.

  If false, the extensions classes are augmented directly to the main class, modifying the main class' prototype.

aggregates <String[]>
  An array of static property names, which will get aggregated on to the built class, in addition to the default properties build will always aggregate as defined by the main class' static _buildCfg property.

Parameters:
  `name <Function>` The name of the new class. Used to defined the NAME property for the new class.
  `main <Function>` The main class on which to base the built class
  `extensions <Function[]>` The set of extension classes which will be augmented/aggregated to the built class.
  `cfg <Object>` Optional. Build configuration for the class (see description).

Returns: `Function`
  A custom class, created from the provided main and extension classes

#### Base.plug

`static void Base.plug ( )`
Alias for Plugin.Host.plug. See aliased method for argument and return value details.

#### Base.unplug

`static void Base.unplug ( )`
Alias for Plugin.Host.unplug. See the aliased method for argument and return value details.

#### destroy

`final Base destroy ( )`

Destroy lifecycle method. Fires the destroy event, prior to invoking destructors for the class hierarchy.

Subscribers to the destroy event can invoke preventDefault on the event object, to prevent destruction from proceeding.

Returns: `Base`
  A reference to this object

Chainable: This method is chainable.

#### init

`final Base init ( config )`
Init lifecycle method, invoked during construction. Fires the init event prior to setting up attributes and invoking initializers for the class hierarchy.

Parameters:
  `config <Object>` Object with configuration property name/value pairs

Returns: `Base`
  A reference to this object

Chainable: This method is chainable.

#### toString

`String toString ( )`
Default toString implementation. Provides the constructor NAME and the instance ID.

Returns: `String`
  String representation for this object

#### Methods inherited from EventTarget:

`_getType, _parseType, addTarget, after, before, bubble, detach, detachAll, fire, getEvent, on, publish, removeTarget, subscribe, unsubscribe, unsubscribeAll`

#### Methods inherited from Attribute:

`_addAttrs, _addLazyAttr, _defAttrChangeFn, _fireAttrChange, _getAttr, _getAttrInitVal, _getAttrs, _getStateVal, _isLazyAttr, _normAttrVals, _protectAttrs, _set, _setAttr, _setAttrs, _setAttrVal, _setStateVal, addAttr, addAttrs, attrAdded, get, getAttrs, modifyAttr, removeAttr, reset, set, setAttrs`

### Events

#### destroy

`destroy ( e )`

Lifecycle event for the destroy phase, fired prior to destruction. Invoking the preventDefault method on the event object provided to subscribers will prevent destruction from proceeding.

Subscribers to the "after" moment of this event, will be notified after destruction is complete (and as a result cannot prevent destruction).

Parameters:
  `e <EventFacade>` Event object

Preventable: This event is preventable by method e.preventDefault(). The default function executed by this event is _defDestroyFn.

#### destroyedChange

`destroyedChange ( event )`
Fires when the value for the configuration attribute 'destroyed' is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:
  `event <Event.Facade>` An Event Facade object with the following attribute specific properties added:
  prevVal
    The value of the attribute, prior to it being set
  newVal
    The value the attribute is to be set to
  attrName
    The name of the attribute being set
  subAttrName
    If setting a property within the attribute's value, the name of the sub-attribute property being set

#### init

`init ( e )`

Lifecycle event for the init phase, fired prior to initialization. Invoking the preventDefault() method on the event object provided to subscribers will prevent initialization from occuring.

Subscribers to the "after" momemt of this event, will be notified after initialization of the object is complete (and therefore cannot prevent initialization).

Parameters:
  `e <EventFacade>` Event object, with a cfg property which refers to the configuration object passed to the constructor.

Preventable: This event is preventable by method e.preventDefault(). The default function executed by this event is _defInitFn.

#### initializedChange

`initializedChange ( event )`
Fires when the value for the configuration attribute 'initialized' is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:
  `event <Event.Facade>` An Event Facade object with the following attribute specific properties added:
  prevVal
    The value of the attribute, prior to it being set
  newVal
    The value the attribute is to be set to
  attrName
    The name of the attribute being set
  subAttrName
    If setting a property within the attribute's value, the name of the sub-attribute property being set

### Configuration Attributes

#### destroyed `- boolean`

Flag indicating whether or not this object has been through the destroy lifecycle phase.
Default Value: false

#### initialized `- boolean`

Flag indicating whether or not this object has been through the init lifecycle phase.
Default Value: false

Copyright © 2009 Yahoo! Inc. All rights reserved.