Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
[ 2.6.2 ]Function in javascript file not recognized
egau
Hello,<br />
<br />
I've created a file "functions.js" that I put in a "JS" folder in my workspace resource folder. I added this file in the Resources part of a rptlibrary ("JS/functions.js"). <br />
<br />
I use a function defined in the JS on a library object. Finaly I add the objet to a rptdesign, the js file is automatically added in the Resources part ("JS/functions.js") of the rptdesign but when I generate the rptdesign, I have an error message telling me that <br />
<br />
"ReferenceError: "formatStuff" is not defined at line 8 of chart script:'' <br />
<br />
Function : <br />
<pre class='_prettyXprint _lang-auto _linenums:0'>function formatStorage( value )</pre>
<br />
Used in a chart object : <br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>value = formatStuff(value);</pre>
<br />
Do I have to import the js somewhere else ? Include something so that it works ?<br />
<br />
Thanks in advance.<br />
<br />
Edit : I checked, and I have <br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'><list-property name="includeScripts">
<property>JS/functions.js</property>
</list-property></pre>
<br />
In the rptdesign XML so I really don't understand why my functions are not recognized -_-
Find more posts tagged with
Comments
CBR
hi,<br />
<br />
the chart and crosstab scripts are the only ones using a different scope as the rest of all the scripts, so you can't easily access a function that is defined in the other scope.<br />
<br />
A possible workaround is to create a persistent global variable:<br />
Put in initialize for every function:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
reportContext.setPersistentGlobalVariable("myFunc", formatStuff());
</pre>
<br />
In chart use<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
function beforeDrawAxisTitle( axis, label, icsc ){
func = icsc.getExternalContext().getScriptable().getPersistentGlobalVariable("myFunc");
func(something);
}
</pre>
<br />
I m not sure but i think that it would be sufficient to use a global variable instead of a persistent global one.
egau
Hello,<br />
<br />
Thanks for the answer, I tried your method but I got some issues :<br />
<br />
As you proposed, I put the in the initialize method of my rptdesign this line : <br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>reportContext.setPersistentGlobalVariable("stringFormatStorage", stringFormatStorage());</pre>
<br />
<br />
Then in the chart object, I put :<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>func = icsc.getExternalContext().getScriptable().getPersistentGlobalVariable("stringFormatStorage");
func(value)</pre>
<br />
<br />
And I got these 2 errors : <br />
<br />
'org.eclipse.birt.report.engine.script.internal.ReportContextImpl.setPersistentGlobalVariable(string,org.mozilla.javascript.Undefined)' cannot be found (/report/method[
@name="
;initialize"]#1)<br />
<br />
and this one ( I guess it's related to the 1st one ) :<br />
<br />
TypeError: func is not a function, it's an object at line 9 of chart script:''<br />
<br />
Do you have an idea ?
egau
Well thanks for your help, it also gave me new keywords for google so I finally found this topic :<br />
<br />
<a class='bbc_url' href='
http://www.birt-exchange.org/org/forum/index.php/topic/20615-report-resource-scripts-in-chart/page__s__b5fb14be2446aff9de6f515a187737d9'>http://www.birt-exchange.org/org/forum/index.php/topic/20615-report-resource-scripts-in-chart/page__s__b5fb14be2446aff9de6f515a187737d9</a><br
/>
<br />
<br />
You mentionned the reason and the solution is , in the chart script ( no need to script anything in the initialize of the report) :<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>var myParameter = 10;
icsc.getExternalContext().getScriptable().setGlobalVariable("myValueParameter", myParameter );
var value = icsc.getExternalContext().getScriptable().evaluate("stringFormatStorage(myGlobalParameter);");</pre>