📜 ⬆️ ⬇️

Invisible.js - some models on both client and server

Invisible.js is a library that allows you to use the same data models on both the client and the server. On the server running on top of express.js, the client is sent via browserify .

What is:

Model description example (available on both client and 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); 

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

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


Details can be viewed on the github-page .
')
P.S. This library is a great example of isomorphic javascript . More to such good!

Source: https://habr.com/ru/post/206526/


All Articles