funcsync
v1.2.1
Published
JavaScript's function client-server synchronization helper library.
Downloads
30
Maintainers
Readme
funcsync
Funcsync is a dependency-free extremely small library helping to exchange/synchronize functions between server and client side.
Prepares an object - might containing business logic as function - for sending in a text format (JSON for example) and vice versa.
Usage:
Server-side
Command line:
npm install funcsync
In node.js code:
var f = require('funcsync');
...
var obj = {
"tab1":[
function test1(){ console.log("My "); },
function test2(){ console.log("dear "); }
],
"tab2":{ "test3": function(){ console.log("user:" + self.name); } }
};
...
f.stringify( obj ) // prepare the given object for sending
...
var fs = f.functify( obj, {nane:'Bob'} ); // retrieve functions from the obj received
fs.tab2.test3();
Bind context
The function functify has an optional second parameter: context. All function will access this context, through the variable name self while being executed.
This way you can bind a knockout.js viewmodel or anything you want and can access it via the name 'self'.
Client-side
In head
<script src='funcsync.min.js'></script>
In any script tag
funcsync.stringify( obj );
For a more complex scenario please find a complex project boilerplate: Division.js, where one business model is defined and maintained allowing you to use the same objects - including model and validation and computed values and associated functions - on both client side, server side and DB interaction!