Sending a javascript object with a key: a couple of functions in a jade template

I am making a jade file like:

On the server side:

var html = jade.renderFile('ui/index.jade', {
    printHello : function(){
       console.log('Hello World');
    }
});

On the client side in index.jade:

script(type="text/javascript").
      var s = #{JSON.stringify(patternMatch)};
      s.printHello(); //Desired to print 'Hello world' on browser console

But it s.printHello()says that the Object object does not have the function 'printHello';

and console.log(s);// gives {} an "empty object";

Why is this so ??

And how do I make a printHellogift and work?

+4
source share
1 answer

this is what i did. I am sending string code from the server

var html = jade.renderFile('ui/index.jade', {
    printHello : "("+"function(){ console.log('Hello World'); }"+")"
});

Then on the client side

script(type="text/javascript").
      var s = !{JSON.stringify(patternMatch)};
      var myFunc = eval(s['printHello'])
      console.log(myFunc()); //prints "Hello World" on client browser
0
source

All Articles