Results 1 to 2 of 2

Thread: Where are the die hard JavaScripter here?

  1. #1
    Join Date
    Jan 2010

    Where are the die hard JavaScripter here?


    I'm struggling with a problem for quite some time and would be happy to finally find a solution. I use JS on the front and backend (Node).

    I store all my functionallity for the backend in one global variable. This is partially true, but for the simplicity of this example, let's go with that

    GLOBAL = {
        base: {
            getTemplates: function() {
                // Contruct a template object
            otherFunction: function() {
                // Do some other cool JS stuff
    Ok, now I'm looking for a way to trigger a particular function on the backend, from the frontend. For the communication between front and back I use socket.io. Socket.io let you pass an object with values.

    I was wondering if I could do something like this

    var o = {
        namespace:  [base][getTemplates]
    io.emit('request', o, function(templates){});
    This way I hope to receive at the backend an object with a value which defines the exact places of the particular function on the backend and contruct the function on the backend like follows:

    io.on('req', function( params) {
        // Append the namespace to our GLOBAL variable to point the exact
        // location of the function, namely GLOBAL.base.getTemplates() and
        // return the result back to the frontend.
        return GLOBAL[params.namespace]();
    Can we solve this problem with something like JSON.stringify or ...

    Any help is more than welcome!!!

    Thanks in advance!!!


  2. #2
    Join Date
    Dec 2011
    Centurion, South Africa
    Hi Christophe27,

    Just my 2c, but you could walk the global variable using a string you pass back.
    In this example, I'm just passing back a string like "base.getTemplates" and then execute the resulting function (which I'm just alerting here):

    var GLOBAL = {
        base: {
            getTemplates: function() {
            	return 'gettemplates result';
            otherFunction: function() {
            	return 'otherfunction result';
    	var i, path = String(params.namespace).split('.'), obj = GLOBAL;
    	for (i = 0; i < path.length; ++i) {obj = obj[path[i]];}
    	return obj();
    	namespace : 'base.getTemplates'
    Hope that gives you some ideas.
    JavaScript: Learn | Validate | Compact | bionoid

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center