Invisible.js

Reusable models in the client and the server

Fork me on GitHub

Invisible.js works on top of your express app leveraging browserify to allow your domain models to work seamlessly in the client and the server.

Invisible = require("invisible");
crypto = require("crypto");
_s = require("underscore.string");

function Person(firstName, lastName, email){
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
}

Person.prototype.fullName = function(){
    return this.firstName + ' ' + this.lastName;
}

Person.prototype.getAvatarUrl = function(){
    cleanMail = _s.trim(this.email).toLowerCase();
    hash = crypto.createHash("md5").update(cleanMail).digest("hex");
    return "http://www.gravatar.com/avatar/" + hash;
}

module.exports = Invisible.createModel("Person", Person);

Require as usual in the server:

Invisible = require("invisible")
john = new Invisible.Person("John", "Doe", "john.doe@mail.com");
john.fullName(); //John Doe

In the client, just add the invisible script:

<script src="invisible.js"></script>
<script>
    jane = new Invisible.Person("Jane", "Doe", "jane.doe@mail.com");
    alert(jane.fullName()); //Jane Doe
</script>

VoilĂ ! Restful API, MongoDB persistence, client and server validations and real-time events all come for free! See the github project for a full reference.