Company API
Unit Constructor
Type: Unit
The main unit constructor.
Syntax
new Unit(descriptor);
Arguments
descriptor
(object) - an object containing one or more properties that will be added to the properties of the unit object. Can also include special properties (see below).
Special Properties
Uses
(object or array) - a set of objects to be added to the unit instance as mixins.Prefix
(string) - a string that will be prepended to all message types broadcasted throughpublish
(see below).initSetup
(function) - a function that will be invoked immediately when the unit instance is created.readySetup
(function) - a function that will be invoked upon window.ondomready.loadSetup
(function) - a function that will be invoked upon window.onload.
Returns
- (unit) a new unit instance.
Function: Unit.isUnit
Checks if an object is a unit or is unit-like.
Syntax
Unit.isUnit(obj);
Arguments
obj
(mixed) - the object to check.
Returns
- (boolean)
true
if the object is a unit instance or a unit-like object,false
if otherwise.
Function: Unit.decorate
Adds unit functionality to an object.
Syntax
Unit.decorate(obj, noEventWrap);
Arguments
obj
(object) - the object to decorate.noEventWrap
(boolean; optional) - if set totrue
, thefireEvent
method of the object (if it exists) will not be wrapped. Defaults tofalse
.
Returns
- (object) the object with unit functionality.
Function: Unit.undecorate
Removes unit functionality from an object.
Syntax
Unit.undecorate(obj);
Arguments
obj
(object) - the object to undecorate.
Returns
- (object) the object with unit functionality removed.
Function: Unit.wrapEvents
Wraps an object’s fireEvent
method to also invoke publish
.
Syntax
Unit.wrapEvents(obj);
Arguments
obj
(object) - the object whosefireEvent
method will be wrapped.
Returns
- (object) the object with a wrapped
fireEvent
method.
Function: Unit.unwrapEvents
Unwraps an object’s fireEvent
method.
Syntax
Unit.unwrapEvents(obj);
Arguments
obj
(object) - the object whosefireEvent
method will be unwrapped.
Returns
- (object) the object with an unwrapped
fireEvent
method.
Unit Instance
Unit Method: setupUnit
Initializes the unit object.
Syntax
unit.setupUnit();
Arguments
- None.
Returns
- (unit) the unit instance.
Notes
- This method is automatically called by the
Unit
constructor as well asUnit.decorate
, so you don’t need to explicitly call it. - If you’re subclassing or mixin-in
Unit
into your classes, you’ll have to call this method as soon as possible.
Unit Method: extendUnit
Adds properties to a unit instance.
Syntax
unit.extendUnit(obj);
Arguments
obj
(mixed) - an object whose properties and methods will be added to the unit instance.
Returns
- (unit) the unit instance.
Notes
- If you pass a class or type constructor function to this method, it will instantiate that class or type first to obtain an object.
Unit Method: getPrefix
Returns the prefix of a unit instance.
Syntax
unit.getPrefix();
Arguments
- None.
Returns
- (string) the prefix of the unit instance.
Unit Method: setPrefix
Sets the prefix of a unit instance.
Syntax
unit.setPrefix(prefix);
Arguments
prefix
(string) - the new prefix for the unit instance.
Returns
- (unit) the unit instance.
Unit Method: isAttached
Checks whether a unit instance is connected to the dispatcher.
Syntax
unit.isAttached();
Arguments
- None.
Returns
- (boolean)
true
if the unit is connected to the dispatcher,false
if otherwise.
Unit Method: attachUnit
Connects a unit to the dispatcher.
Syntax
unit.attachUnit();
Arguments
- None.
Returns
- (unit) the unit instance.
Unit Method: detachUnit
Disconnects a unit to the dispatcher.
Syntax
unit.detachUnit();
Arguments
- None.
Returns
- (unit) the unit instance.
Unit Method: publish
Broadcasts a message to all units.
Syntax
unit.publish(type, data, finalized);
Arguments
type
(string) - the type of message to be published.data
(mixed, optional) - the data associated with the message that will be passed to subscribers.finalized
(boolean, optional) - if set to true, themessage
will be broadcasted as final.
Returns
- (unit) the unit instance.
Notes
- If your unit has a prefix set, the prefix will automatically be prepended to the value of the
type
argument. - If you want to bypass the auto prefixing behaviour, prepend the value of the
type
argument with an exclamation point (!
).
Unit Method: subscribe
Subscribes a unit to a message type.
Syntax
unit.subscribe(type, fn);
Arguments
type
(string) - the type of message to subscribe to.fn
(function) - the function to execute when a message of thetype
is received.
Returns
- (unit) the unit instance.
Notes
- Prepending an exclamation point (
!
) to thetype
string argument will cause a replay: the last message of that type will be rebroadcasted for the current unit.
Unit Method: unsubscribe
Unsubscribes a unit from a message type.
Syntax
unit.unsubscribe(type, fn);
Arguments
type
(string) - the type of message to unsubscribe to.fn
(function, optional) - the function to execute when a message of thetype
is received.
Returns
- (unit) the unit instance.
Notes
- You must supply this method with exactly the same
fn
argument you used with thesubscribe
method in order for it to remove a single callback properly. - Calling this method without a second argument will remove all callbacks for a type.