milo

ClassRegistry

function
 ClassRegistry() 

Option name Type Description
FoundationClass Function

All classes that are registered in the registry should be subclasses of the FoundationClass

return Object

milo.classes.ClassRegistry - the registry of classes class.
Components and Facets register themselves in registries. It allows to avoid requiring them from one module and prevents circular dependencies between modules.

function ClassRegistry (FoundationClass) {
    if (FoundationClass)
        this.setClass(FoundationClass);

    this.__registeredClasses = {};
}

####ClassRegistry instance methods####

_.extendProto(ClassRegistry, {
    add: add,
    get: get,
    remove: remove,
    clean: clean,
    setClass: setClass
});

add

function
 add() 

Option name Type Description
aClass Function

class to register in the registry. Should be subclass of this.FoundationClass.

name String

Optional class name. If class name is not specified, it will be taken from constructor function name. Class name should be a valid identifier and cannot be an empty string.

ClassRegistry instance method that registers a class in the registry.
The method will throw an exception if a class is registered under the same name as previously registered class.
The method allows registering the same class under a different name, so class aliases can be created.

function add(aClass, name) {
    name = name || aClass.name;

    check(name, Match.IdentifierString, 'class name must be identifier string');

    if (this.FoundationClass) {
        if (aClass != this.FoundationClass)
            check(aClass, Match.Subclass(this.FoundationClass), 'class must be a sub(class) of a foundation class');
    } else
        throw new Error('foundation class must be set before adding classes to registry');

    if (this.__registeredClasses[name])
        throw new Error('class "' + name + '" is already registered');

    this.__registeredClasses[name] = aClass;
}

get

function
 get() 

Option name Type Description
name String

Class name

return Function

Gets class from registry by name

function get(name) {
    check(name, String, 'class name must be string');
    return this.__registeredClasses[name];
}

remove

function
 remove() 

Option name Type Description
nameOrClass String, Function

Class name. If class constructor is supplied, its name will be used.

Remove class from registry by its name.
If class is not registered, this method will throw an exception.

function remove(nameOrClass) {
    check(nameOrClass, Match.OneOf(String, Function), 'class or name must be supplied');

    var name = typeof nameOrClass == 'string'
                        ? nameOrClass
                        : nameOrClass.name;
                        
    if (! this.__registeredClasses[name])
        throw new Error('class is not registered');

    delete this.__registeredClasses[name];
}

clean

function
 clean() 

Removes all classes from registry.

function clean() {
    this.__registeredClasses = {};
}

setClass

function
 setClass() 

Option name Type Description
FoundationClass Function

Any class that will be added to the registry should be a subclass of this class. FoundationClass itself can be added to the registry too.

Sets FoundationClass of the registry. It should be set before any class can be added.

function setClass(FoundationClass) {
    check(FoundationClass, Function);
    _.defineProperty(this, 'FoundationClass', FoundationClass, _.ENUM);
}