Evaluate custom javascript method (CircularJSON) with Jade - javascript

I want to parse an object into client-side javascript through Jade. Normally this would work:
script var object = JSON.parse(#{JSON.stringify(object)});
but my object is circular and I need to do this
script var object = CircularJSON.parse(#{CircularJSON.stringify(object)});
but it throws the error
Cannot call method 'stringify' of undefined
which I guess is because Jade doesn't recognise my CircularJSON method.
Any way to make it?

It could be required and passed in the locals
response.render("index.jade", {CircularJSON : require('circular-json')});
Or it could be defined as a function in the scope of jade
- var CircularJSON = function(e,t){function l(e,t,o){var u=[],...//whole function
script var player = CircularJSON.parse('!{CircularJSON.stringify(player)}');

Related

How can I debug evaled JavaScript within AngularJS?

I have some arbitrary javascript code that is run via the following method:
download it onto client browser via ajax call
pass resulting code string into javascript eval function.
Note: I cannot download/run the code by adding its file URL to a <script>'s src property because the code must run within a particular scope.
I would like to debug this code in Google Chrome. I am able to do this using plain Javascript, but not when I am using AngularJS. How can I fix this?
Example 1 (plain JS, I am given option to inspect code.js):
var codeToRun = ""
+"var a = 1;\n"
+"var b = 2;\n"
+"This line will fail\n"
+"//# sourceURL=code.js";
eval(codeToRun);
result: http://i.imgur.com/3LL9xi7.png
demo: https://jsfiddle.net/AlexLeung/mpjrwmbm/
Example 2 (AngularJS, I am not given option to inspect code.js):
var codeToRun = ""
+"var a = 1;\n"
+"var b = 2;\n"
+"This line will fail\n"
+"//# sourceURL=code.js";
angular.module("myApp", [])
.controller("myController", function($scope) {
eval(codeToRun);
});
result: http://i.imgur.com/x7lZcUf.png
demo: http://jsfiddle.net/oeoaep0o/
Use a named function rather than an anonymous function. For example:
var codeToRun = "var a = 1; console.log(a) //# sourceURL=foo.js";
function evaler()
{
eval(codeToRun);
}
angular.module("foo", []).run(evaler);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="foo"></div>
References
Named versus Anonymous Functions: Angular Style Guide
sourceURL and displayName in action: Eval and anonymous functions

Using Javascript reportContext.setPersistentGlobalVariable method on Birt handlers

I trying to use reportContext.setPersistentGlobalVariable to define global variable on the OnFetch Javascript method of the data set like that:
flag = 1;
if(row.Percent>10)
reportContext.setPersistentGlobalVariable("flag", flag);
and to retrieve the variable on the beforeRender method like that:
var flg = reportContext.getPersistentGlobalVariable("flag");
if(flg==1)
reportContext.getDesignHandle().findElement("chartToHide").drop();
but by running the report receiving this error:
org.eclipse.birt.report.engine.api.EngineException: There are errors evaluating script "reportContext.setPersistentGlobalVariable("flag", true);":
Fail to execute script in function __bm_onFetch().
In theory this code should work because the method setPersistentGlobalVariable expects a serializable object:
void setPersistentGlobalVariable( String name, Serializable obj );
but in practice in Rhino scripts it seems it can only handle a String, try this:
var flag = "1";
if(row.Percent>10)
reportContext.setPersistentGlobalVariable("flag", flag);
var flg = reportContext.getPersistentGlobalVariable("flag");
if(flg=="1")
reportContext.getDesignHandle().findElement("chartToHide").drop();

ExecJS - Javascript object instances in Ruby?

If I have a javascript object, I would normally interact with the object and its methods like this:
var obj = someObject.getInstance();
var result = obj.someMethod();
where someMethod is defined like this:
someObject.prototype.someOtherMethod = function() { //do stuff };
someObject.prototype.someMethod = function(foo) { this.someOtherMethod(); };
However, I am getting an error when I want to call someMethod in Ruby via ExecJS:
context = ExecJS.compile(# the javascript file)
context.call('someObject.getInstance().someMethod')
# Gives a TypeError where Object has no method 'someOtherMethod'
On the other hand, functions that are defined in the javascript module are working fine:
someFunction = function() { // do stuff };
# in Ruby
context.call('someFunction') # does stuff
Can ExecJS handle Javascript objects and their methods, or am I only able to call functions with it?
With regards to the specific application, I am looking into https://github.com/joenoon/libphonenumber-execjs, but the parse function in Libphonenumber does not work for the above reason.
Discovered the answer through some experimentation. I managed to get the desired functionality by using context.exec() instead of call.
js = <<JS
var jsObj = someObject.getInstance();
var res = jsObj.someMethod();
return res;
JS
context.exec(js);
However, if your method returns a Javascript object, you have to serialize it first or otherwise parse the results so that it can be returned by ExecJS into a suitable Ruby object.

jasmine: using function with a dot in the name

I'm testing a js function that uses functions from other js files.
One of my external js files has a function defined as such:
functionname.functionextension = function () {.....}
when testing using jasmine, and calling functionname.functionextension, it complains that functionname is not defined. I think it believes that functionname is an object..
I know that one way to get around this is to modify the function name but I can't do that. Is there any other way?
Thanks
In javascript, all functions are objects. In the external js file, the function is probably defined like this:
var functionname = functionname || {};
functionname.functionextension = function () {
...
};
If you're getting a script error that functionname is not defined, there is either an error in the external javascript or you are not calling some initialization function that the external script requires to set up its objects.
It worked for me...u need to call function by its full name like functionname.functionextension() while calling.

why does this object declaration work in node.js and how to declare a static variable?

i have seen this code :
var myNet = require ("net");
and in some function:
function foo (x,y) {
var myNewNet = new myNet();
myNewNet.createServer(x,y);
}
why does the code above create a new object? what is the mechanism stands behind that?
one more question, how do i create a static var in node.js, for example a id number that has to be unique.
i came with this option for static variable:
var id =0;
and put it on the global scope, is it ok?
The require statement basically is like an import; it takes an external library and makes it available in your code.
If you ever look in an external module, you will notice that it's just normal node.js js code. It has EXPORT statements in it. Those statements are what gets made available when you require something. Check out http://howtonode.org/creating-custom-modules
There is a GLOBAL keyword in node.js you can use to make something global
GLOBAL.IP_ADDRESS = "..."
As #Raynos says, it's not usually a good idea to do that, so another options is to export a constant from a module, so you can create a module and do
exports.STATIC_CONSTANT = "";
and then once you import the module you can do
var mod = require('mymodule');
mod.STATIC_CONSTANT;
EDIT, to answer you comment, the line
var myNet = require("net")
causes myNet to be whatever the net module exports. It must be exporting a function, so
var newNet = new myNet()
creates a new instance of the net object. From there
myNewNet.createServer()
is just invoking a method on the object you just created.

Categories