vitruvio

1.1.0 • Public • Published

Vitruvio

Is a framework which extends JavaScript capabilities in order to allow developing OOP applications over an structural well designed architecture by defining: namespaces, classes, interfaces, enumerators, inheritance, exceptions and other resources. Altogether with the solid class system proposed, it offers:

  • An extensible Exception hierarchy
  • Event management system
  • Genericity checkouts over objects.
  • JXON and XML parser.
  • JSONP, AJAX and WebSocket client implementations
  • Basic DOM utility classes (Browser only).
  • A dynamic and extensible resources loader system highly compatible over web browsers and node js which allows customizations.
  • Cryptography encoder/decoder (UTF8, Base64).

Environments and browser support

Vitruvio runs server side on Node.JS and client side suports the following browsers on desktop and mobile devices:

  • Google Chrome
  • Microsoft Edge
  • Mozilla Firefox
  • Opera
  • Safari
  • Internet Explorer ver. 5+

How to use

Step 1, get it down!

Over NodeJS it can be easyly downloaded and installed with:

npm install vitruvio --save

or can be downloaded via Right click/Save as: latest realeased

Step 2, include it in your code!

Vitruvio exports the System namespace which is the main namespace of the framework and contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions. For this reason is highly recommended to name it on source code as System.

On NodeJS using require:

var System = require('vitruvio');

On html pages:

<...>
<script type="text/javascript" src="./public/js/vitruvio/vitruvio.min.js"></script>
<script>
  // System is already loaded or you can use System.ready(function)
  System.ready(function(System) {
    // Do some cool things...
  })
</script> 
<...>

Step 3, get System's globals

var using = System.using; // Resources solver function
 
// Main functions and resources
var Namespace = using('System.Namespace');
var Class = using('System.Class');
var Interface = using('System.Interface');
var Enum = using('System.Enum');
...

Step 4, lets define some classes!

// on file src/com/example/Animal.js
Namespace('com.example', 
    /**
     * Animal class
     */
    Class('Animal', {
      'constructor' : function(specie) {
          this.specie = specie;
      },
      'getSpecie' : function() {
          return this.specie;
      }
    })    
)
 
// on file src/com/example/Dog.js
Namespace('com.example', 
    /**
     * Dog class
     */
    Class('Dog', {
      '$extends' : 'com.example.Animal',
      'constructor' : function(name, race, age) {
          this.$super('canis'); // initialize super class' constructor first
          this.name = name;
          this.race = race;
          this.age = age;
      },
      'getName' : function() {
          return this.name;
      },
      'getAge' : function() {
        ...
      },
      /**
       * @Override getSpecie
       */
      'getSpecie' : function() {
          return this.$super.getSpecie() + " - " + this.race;
      }
    })    
)
 
// on file src/main.js
 
// Load and get the Dog class reference asynchronously
using('com.example.Dog', function(Dog){
    var boxer = new Dog("Snoopy", "Boxer", 5);
 
    console.log(boxer.getName()) // Snoopy
    console.log(boxer.getSpecie()) // canis - Boxer
});
 

Once a class has been loaded, you can get its reference:

var MyClass = using('com.example.MyClass');

Is and As operators

One of the most advanced capabilities of Vitruvio framework is the ability to cast objects to a specific type and to check instances out by its type.

Is operator example: Pure JavaScript:

try {
  ...
} catch(e) {
    if(e.type == "Error") {
        // Do something
    }
    
    // or 
    
    if(instanceof Error) {
        // Do something
    }
}

Vitruvio schema:

try {
  // Some code which throws an error.
  throw new CustomException("My Custom message");
} catch(e) {
    // The is operator in most cases will do the same as the instanceof JavaScript operator.
    if(e.is(CustomException)) {
      // Do something
    } else if(e.is(Exception)) { // Base System.Exception class
      // Do something
    } else { // Third party and basic JavaScript errors
      // Do something
    }
}

As operator: Pure JavaScript:

...

Vitruvio schema:

// on file src/com/example/ifaces/Runnable.js
 
Namespace('com.example.ifaces', 
  Interface('Runnable', {
    'run': function(task) {}
  })
)
 
// on file src/com/example/tasks/DownloadTask.js
Namespace('com.example.tasks', 
  Class('DownloadTask', {  
    '$implements' : 'com.example.ifaces.Runnable',
    'taskList' : [],
    'run': function(task) {
        taskList.push(task);
        // ...
    },
    'getTasks' : function() {
        return this.taskList;
    },
    // several methods and properties
  })
)
 
// on main.js file
var Runnable = using('com.example.ifaces.Runnable');
var DownloadTask = using('com.example.tasks.DownloadTask');
 
var task = new DownloadTask();
 
var runnable = task.as(Runnable); // the casted runnable instance will only contain the run method.
 
 

Dependencies (0)

    Dev Dependencies (8)

    Package Sidebar

    Install

    npm i vitruvio

    Weekly Downloads

    9

    Version

    1.1.0

    License

    Apache-2.0

    Last publish

    Collaborators

    • yadirhb