VNLS

Small javascript framework

npm install VNLS
19 downloads in the last week
57 downloads in the last month

VNLS

VNLS is small javascript framework. It's purpose is to provide boundaries to the very flexible Javascript language and provides a way to write code in a consistent and uniform manner that can be used on the server, Nodejs AND in the browser.

And don't we all like to 'Write once and use many'? Consider this framework as an experiment in multi-platform coding.

It provides:

  • A simple mechanism for dealing with name-spaces.
  • A better typeof.
  • A clear separation between Javascript objects (the classical OOP approach with inheritance) and singleton patterns.
  • An event model (reverse engineered from nodejs's eventemitter) for use in other environments
  • Promisses/Futures.
  • It does not, extend the javascript core language or any other object, except for some ES5 methods

Install:

Nodejs:

On node nodejs npm install VNLS

Then, somewhere at the top of your code

require('VNLS');

There is no need to bind it to a var because var VNLS is made global to the nodejs process so it's behavior is exactly like in a browser environment. All-though this IS a kind of pollution on nodejs.

Browser

In a browser (you may also download the file first):

 <script type="text/javascript" src="https://github.com/venalis/VNLS/blob/master/lib/vnls.min.js"></script>

Usage:

Writing a class:

A class is an object definition but never used directly. We are going to write a class in the 'test' namespace. The __init__ method is called upon creation.

          VNLS.addClass('test',function(){
            function MyClass() {
            };
            MyClass.prototype.__init__ = function(str){
                 this.text = str;
            }      

            MyClass.prototype.getText = function(){
                return this.text
            }
            return MyClass;
        });

Using the class

We 'r going to use the previous class twice: the method getObject must have least 2 arguments.

  1. the namespace
  2. the class_name
  3. optional arguments (passed to the __init__ method)

To create an object:

  var my_class_1 = VNLS.getObject('test',"MyClass","foo");
  var my_class_2 = VNLS.getObject('test',"MyClass","bar");

  console.log(my_class_1.getText());
  console.log(my_class_2.getText());

I wrote a Promise as an example This object is extended with a EventEmitter.

Extending a namespace

A namespace is used to extend the global VNLS object. All it's methods are therefor singletons.

VNLS.ns('my.test',{
   myMethod : function(){
     return 'test';
   }
})

Access a namespace

VNLS.my.test.myMethod();

or

VNLS.ns('my.test').myMethod()

Dependencies:

In normal use there are no dependencies, it will run in the browser and on nodejs out of the box.

Development:

When contributing, experimenting or if you want to build your own, use the following node_modules.

  • docco Documentation npm install docco
  • jasmine-node Testing npm install jasmine-node
  • nodewatch Autotesting TDD npm install nodewatch
  • uglify-js Building npm install uglify-js
  • jake rake, make, ant substitute (Java-script all the way) npm install -g jake

For jake these are the commands:

  • jake build : build 2 versions (1 minified) in './lib/*'
  • jake test : run the specs
  • jake autotest : run the specs on a filechange for TDD
  • jake docs : generate documentation in ./docs

For testing in a browser environment just point your browser to 'specs/index.html'. It should run the same specs as jake test does. (The tests cover both browser and server)

TODO:

  • Write some examples and howtos in the wiki
  • Clean up the specs
  • Refactor some bits following Felix's coding guidelines.
npm loves you