DCL

An elegant OOP with mixins + AOP for JavaScript.

Destroyable

Destroyable forces all methods called destroy() to be chained in the “before” fashion (opposite to constructors). It is meant to provide a destruction foundation, so objects can be destroyed explicitly in a unified fashion.

It can be included with following commands:

Include Destroyable
1
2
3
4
5
6
7
8
9
10
11
12
13
// node.js
var Destroyable = require("dcl/mixins/Destroyable");
...

// AMD (code)
require(["dcl/mixins/Destroyable"], function(Destroyable){
  ...
});

// AMD (definition)
define(["dcl/mixins/Destroyable"], function(Destroyable){
  ...
});

Description

Here is its definition:

Destroyable
1
2
3
4
var Destroyable = dcl(null, {
  declaredClass: "dcl/mixins/Destroyable"
});
dcl.chainBefore(Destroyable, "destroy");

As you can see it containes no code! The only important part is a chaining directive. When you mix it in to your “classes” it will weave all found destroy() methods properly.

Read a background on destruction in Destructors.

Examples

Destroyable example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var A = dcl(Destroyable, {
  declaredClass: "A",
  constructor: function(){
    // our controlled resource:
    this.node = document.createElement("div");
    // if we do not remove it from a document,
    // when deleting an instance, it will persist
    ...
  },
  destroy: function(){
    // we should remove our controlled resource
    // from a document
      if(this.node && this.node.parentNode){
      this.node.parentNode.removeChild(this.node);
      this.node = null;
    }
  },
  ...
});

Fork me on GitHub