• Meetings e Workshop
    l'associazione organizza workshop e meeting periodici

  • HowTo e manuali
    come costruire da te gadget elettronici e come programmarli

  • Hacking della PS3
    howto su come installare linux ed effettuare altri hack

  • Una comunità organizzata
    entra a far parte di una associazione organizzata e attiva

  • Strumenti di supporto
    per mantenersi in contatto e favorire la cooperazione

snippet - sharing codebase between javascript clientside and javascript serverside - intermediate PDF Stampa E-mail
Scritto da Carlo Denaro - Blackout   
Carlo Denaro - Blackout

How sharing javascript between javascript clientside and javascript serverside?

If we want to develop a software with hexagonal architecture principle we can found this problem (in javascript).

We share same core code with "frontend" adapter and "cli" adapter (for example).

For example we have a core code like:

Core.js

var myFunction = function (var){ /* do stuff */ };

And we have adapter

FrontendAdapter.js

var view = Backbone.View.extend({
initialize: function(injected) {
this.injectCore = injected;
}
});

And we have another server side adapter (like cli)

CliAdapter.js

const core = require('Core');
core('var');

How we can share Core.js between CliAdapter and FrontendAdapter?

It's easy. We add at end of Core.js this code

if (typeof module !== 'undefined' && module.exports) {
module.exports = myFunction;
}

And when CliAdapter include Core.js you have export, otherwise when you use Core.js in browser or in test suite (with jasmine), module.exports never trigged.

Happy Hacking.


 

Aggiungi commento


Codice di sicurezza
Aggiorna


grayhats.org, Powered by Joomla!; Joomla templates by SG web hosting
valid xhtml valid css