# Yahoo! UI Library

### base 3.3.0

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

## Class Base - uses Attribute , Plugin.Host

Known Subclasses:
  CartesianSeries DataSource.Local Widget DD.Delegate Gridlines LeftAxisLayout Plugin.EditorLists DD.Drop RightAxisLayout ImgLoadGroup Recordset Plugin.EditorTab Cache ImgLoadImgObj Plugin.EditorBR Plugin.DDConstrained Columnset DD.Drag Plugin.ExecCommand EditorBase Plugin.Sortable Frame Plugin.Base Anim DD.DDM Resize Plugin.NodeFX Plugin.SortScroll Plugin.EditorPara Plugin.DDProxy Plugin.EditorBidi DD.Scroll

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. The object can be used to provide default values for the objects published attributes.

  The config object can also contain the following non-attribute properties, providing a convenient way to configure events listeners and plugins for the instance, as part of the constructor call:

  on
    An event name to listener function map, to register event listeners for the "on" moment of the event. A constructor convenience property for the on method.
  after
    An event name to listener function map, to register event listeners for the "after" moment of the event. A constructor convenience property for the after method.
  bubbleTargets
    An object, or array of objects, to register as bubble targets for bubbled events fired by this instance. A constructor convenience property for the addTarget method.
  plugins
    A plugin, or array of plugins to be plugged into the instance (see PluginHost's plug method for signature details). A constructor convenience property for the plug method.

### 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 Plugin.Host:

`PluginHost`

#### 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.

#### _preInitEventCfg

`private void _preInitEventCfg ( config )`
Handles the special on, after and target properties which allow the user to easily configure on and after listeners as well as bubble targets during construction, prior to init.

Parameters:
  `config <Object>` The user configuration object

#### 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

Deprecated Use the more convenient Base.create and Base.mix methods instead

#### Base.create

`static Function Base.create ( name , main , extensions , px , sx )`

Creates a new class (constructor function) which extends the base class passed in as the second argument, and mixes in the array of extensions provided.

Prototype properties or methods can be added to the new class, using the px argument (similar to Y.extend).

Static properties or methods can be added to the new class, using the sx argument (similar to Y.extend).

Parameters:
  `name <Function>` The name of the newly created class. Used to defined the NAME property for the new class.
  `main <Function>` The base class which the new class should extend. This class needs to be Base or a class derived from base (e.g. Widget).
  `extensions <Function[]>` The list of extensions which will be mixed into the built class.
  `px <Object>` The set of prototype properties/methods to add to the built class.
  `sx <Object>` The set of static properties/methods to add to the built class.

Returns: `Function`
  The newly created class.

#### Base.mix

`static Function Base.mix ( main , extensions )`

Mixes in a list of extensions to an existing class.

Parameters:
  `main <Function>` The existing class into which the extensions should be mixed. The class needs to be Base or class derived from base (e.g. Widget)
  `extensions <Function[]>` The set of extension classes which will mixed into the existing main class.

Returns: `Function`
  The modified main class, with extensions mixed in.

#### 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 guid, if set.

Returns: `String`
  String representation for this object

#### Methods inherited from EventTarget:

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

#### Methods inherited from Attribute:

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

#### Methods inherited from Plugin.Host:

`_destroyPlugins, _initPlugins, hasPlugin, plug, unplug`

### 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 © 2011 Yahoo! Inc. All rights reserved.