funcsync

JavaScript's function client-server synchronization helper library.

npm install funcsync
12 downloads in the last week
50 downloads in the last month

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!

npm loves you