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.
- It does not, extend the javascript core language or any other object.
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://raw.githubusercontent.com/jorritd/vnls/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.
- the namespace
- the class_name
- 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());
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';
}
})
or:
VNLS.ns('private',(function(){
var privateFn = function(){
.....
}
var api = {
exposeMe: function(){
}
}
return api;
})());
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.
- 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 compress
: build minified version in './lib/*'jake test
: run the specsjake autotest
: run the specs on a filechange for TDD
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)