DCL

An elegant OOP with mixins + AOP for JavaScript.

registry

Version 2.x

registry plugs in to dcl and automatically register all new constructors using declaredClass property as a key. Users can access constructors by name, enumerate them, and clear the registry selectively by name, or whole.

It is defined in dcl/utils/registry.js.

Description

registry returns a singleton with following API:

registry
1
2
3
4
5
6
7
{
  get:    function (name) {...},
  has:    function (name) {...},
  delete: function (name) {...},
  keys:   function () {...},
  clear:  function () {...}
}

The registry is updated automatically every time new constructor is created with dcl, if it defines a name using declaredClass property.

get(name)

get(name) takes a constructor name, and returns a constructor function, or undefined, if it is not found.

has(name)

has(name) takes a constructor name, and returns true, if the registry contains such constructor, or false otherwise.

delete(name)

delete(name) takes a constructor name, and deletes the corresponding constructor from the registry. The method returns true, if the operation was successful, or false otherwise.

keys()

keys() returns an array of all constructor names in the registry.

clear()

clear() purges the registry from all registered constructors.

Examples

Using registry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var registry = require('dcl/utils/registry');

// next constructor will be registered automatically
var A = dcl({
    declaredClass: 'A'
    // ...
  });
console.log(registry.has('A'));    // true
console.log(!!registry.get('A'));  // true

// let's unregister it
console.log(registry.delete('A')); // true
console.log(registry.has('A'));    // false

// ...

// enumerate all constructors
registry.keys().forEach(function (name) {
    var Ctr = registry.get(name);
    // do something with Ctr
  });

// delete all constructors that start with 'Debug'
registry.keys().
  filter(function (name) { return /^Debug/.test(name); }).
  forEach(function (name) {
    registry.delete(name);
  });

// access a constructor by name
if (registry.has('Component')) {
  var Component = registry.get('Component');
  var component = new Component();
  // use component
}

// remove all constructor registered so far
registry.clear();
console.log(registry.keys().length); // 0