CanJS - number of controller instances

by ryanwheale

HTML

<script src="http://canjs.com/release/2.0.4/can.jquery.js"></script>

JavaScript

$(function() {
    var MyController = can.Control.extend({
        /* constructor */
        instances: 0
    }, {
        /* prototype */
        init: function() {
            this.constructor.instances++;
        },
    
        destroy: function() {
            this.constructor.instances--;
        }
    });
    
    console.log("There are " + MyController.instances + " instances of MyController.");
    
    var controllerInstance;
    if( MyController.instances === 0 ) {
        controllerInstance = new MyController();
    }
    
    console.log("There are NOW " + MyController.instances + " instances of MyController.");
    
    controllerInstance.destroy();
    
    console.log("And now there are " + MyController.instances + " instances of MyController.");
});