Modules and Classes
CAAT is Object Oriented. Classes (constructor functions), constants and its names are defined declaratively using CAAT's module manager: MoMa.
Every CAAT Class object is defined in a module. A module creates a new Class, can extend one sinle Class, defines Class level constants, declares dependencies and a list of Class aliases. MoMa will take care of loading dependency files and solving them.
CAAT can be bundled either in one single file instead of a file per module too.
You can find CAAT bulk bundled library files under the "build" folder. These files contain all CAAT's modules and classes.
A module is defined as follows:
CAAT.Module( { defines : "CAAT.Foundation.UI.StarActor", aliases : ["CAAT.StarActor"], depends : [ "CAAT.Foundation.ActorContainer" ], extendsClass : "CAAT.Foundation.ActorContainer", extendsWith : function() { return { __init : function() { this.__super(); this.compositeOp= 'source-over'; return this; }, ... } }, decorated : false });
This definition is for the StarActor object. MoMa will build a Class object (constructor function) and assign it to CAAT.Foundation.UI.StarActor, as well as to the aliases collection CAAT.StarActor. Both objects are the same constructor function. The aliases block is optional.
To build a new ShapeActor, you can call new CAAT.StarActor() or new CAAT.Foundation.UI.StarActor interchangeably.
This Class depends on CAAT.Foundation.ActorContainer, meaning that before building this object class, CAAT.Foundation.ActorContainer and its dependencies will be loaded and solved. The depends block is optional.
The extendsClass means a ShapeActor is an ActorContainer, and can have other Actors contained inside. This is a kind of Object Oriented extension mechanism where ShapeActor has all of ActorContainer's functionality. The extendsClass block is optional.
The extendsWith clause can be either a function, which must return an object that will be used as the new Class prototype (and possibly extending the base Class defined in the extendsClass clause).
CAAT Class definition does not rely on the Constructor function to perform initialization. Instead, an special function can be defined which must hold construction code. This function is __init and if present is called from the constructor function. If you want to chain constructors, the correct way of doing it is by calling this.__super(arg1, arg2, ...);.
The decorated clause instruments MoMa to create a decorated Class, which will be slower in execution, but more maintainable. When set to true, MoMa will make all base class' overriden methods have a __super attribute. So calling any overriden method will be via: this.__super(arg1, arg2, ...). If decorated is set to true (default value), calling a super class' overriden method will be faster in execution, but uglier. For example, for the ShapeActor example you must call:
CAAT.Foundation.UI.ShapeActor.superclass.method.call(this, arg1, arg2, ...);
Creating new Modules
For any given Class object, you could extend it, either by defining a new module which sets an appropriate extendsClass clause or use the legacy extend(subclass, superclass) function.
MoMa module:
CAAT.Foundation.UI.ShapeActor.extend( { abcd : 1234 }, { constant : 1 }, "MyNewClass", [ "MyNewClassAlias1" ], decorated : true_or_false );
This code will create a new Class called MyNewClass, which extendsClass CAAT.Foundation.UI.ShapeActor with abcd:1234, defines a constant: CAAT.Foundation.UI.ShapeActor.constant= 1, creates an alias MyNewClassAlias1, etc.
From this point, calling new MyNewClass or new MyNewClassAlias1 will have the same effect and is creating a new Actor instance.
Calling legacy extend function
The following code:
(function() { HN.Chrono= function() { HN.Chrono.superclass.constructor.call(this); return this; }; HN.Chrono.prototype= { maxTime: 0, elapsedTime:0, animate : function(director, time) { var size= this.maxTime!=0 ? this.elapsedTime/this.maxTime * this.progressHole : 0; // make sure this actor is marked as dirty by calling setSize and not .width=new_size. this.actorcrono.setSize( this.progressHole-size, this.actorcrono.height ); return HN.Chrono.superclass.animate.call(this,director,time); } }; extend( HN.Chrono, CAAT.ActorContainer); })();
Will create a new Class: HN.Chrono which extendsClass CAAT.ActorContainer. It has the same effect as calling extend on a Module, or creating a CAAT.Module definition. This is the way CAAT objects were extended and has full compatibility with current Modules definition.
Both ways of creating new Actor classes are valid. It is preferred though using the Module method.