Support #3976
closedJavaScript variables scope
0%
Description
I would like to know if there is some way to write global javascript variables with WT. I'm trying to use a framework named XTK of JavaScript. I need to create some variables to manipulated them later, but when I create the variables and I try to use them in others methods, I have errors in console, It tells me that te variables do not exist.
Updated by Koen Deforche over 9 years ago
Hey,
There is nothing special about JavaScript executed within the scope of a Wt application. If you create a global variable, then then you should be able to access it later.
Koen
Updated by Johann Gonzalez over 9 years ago
Hi... Well then I thing I'm doins something wrong. As I said, Im' working with a framework named XKT. In a method I use something like
Wt::WContainerWidget* w = new Wt::WContainerWidget(parent);
Wt::WText* text = new Wt::WText("",w);
Wt::WText* text = new Wt::WText("",w);std::string cadena =\" \
var r = new X.renderer3D(); \
r.container = '\" text->id() "'; \
r.init(); \
var skull = new X.mesh(); \
skull.file='http://x.babymri.org/?skull.vtk';\\
r.add(skull); \
r.render() \
";
w->doJavaScript(cadena);
I used that for creeate an image 3d. After thatn in other method I call the JS variable r and skull, and they are not defined. I do not know if I should use an other different method or something.
I already tried changin the JS code, includind the word "window." in the definition of the variables but then, the image does not even load
Merci
Updated by Wim Dumon over 9 years ago
- Status changed from New to Resolved
Hello,
The easiest way is to tie this object to the DOM object in which it belongs, so in your case:
Wt::WContainerWidget* w = new Wt::WContainerWidget(parent);
Wt::WText* text = new Wt::WText("",w);
Wt::WText* text = new Wt::WText("",w);
std::string cadena =" \
var r = new X.renderer3D(); \
" + jsRef() + ".r = r; \
r.container = '" text->id() "'; \
r.init(); \
var skull = new X.mesh(); \
skull.file='http://x.babymri.org/?skull.vtk';\
r.add(skull); \
r.render() \
";
w->doJavaScript(cadena);
jsRef() creates a JS string that refers to the WText's DOM element (DIV), and r is now also a member of this element, so you can refer it as long as the DOM element exists through the string 'jsRef() + ".r"'.
BR,
Wim.
Updated by Johann Gonzalez over 9 years ago
I have been worked in this, and in the beggining it worked good. But then I found the following problem.
I'm using JS code everywhere in my code. So I have a class c that create one element JS (render), this is gonna add images JS that are created in others classes c. Then I create the render, after that, the code that creates the images is excecuted, I test this part with som couts in the same class and it works, the images are created. After that, i one method I try to use these images but JS tell me that those variables are not defined. I attach the differenst part of my code to make it easy to understand.
1. Create the render
Wt::WContainerWidget *w = new Wt::WContainerWidget(parent);
std::string cadena1 =\" \
var mRender = new X.renderer3D(); \
mRender.container = '\" w~~id()+ "'; \
mRender.init(); \
" + w~~>jsRef() ".mRender = mRender; ";
w->doJavaScript(cadena1);
2. Creation of images
std::string cadena = \" \
var mObject = new X.\" + tipo+ "(); \
"w2->jsRef()".mObject = mObject;";
w2->doJavaScript(cadena);
w~~doJavaScript(w~~>jsRef() + ".mObject.file = '" + bbGetInputImage() + "';");
3 renderer
cadena2 = cadena2 + "" + w~~jsRef() + ".mRender.add(" + w2~~>jsRef() + ".mObject);";
cadena2 = cadena2 + "" + w->jsRef() + ".mRender.render();";
w->doJavaScript(cadena2);
But the console of Mozilla tells me that W.....mObject is not defined. The code is not actually that but is the idea.
The 1st and the 3rd part are in the same file in differents methods, and the 2nd part is in an other different file.
The container W2 exist but never is added in the page, that does not matter cuz it did not work when I added it anyway. I hope you guys can help me.
Thanks very much
Updated by Johann Gonzalez over 9 years ago
Forget all of that, I already found the problem and it's mine, thanks anyway
Updated by Koen Deforche about 9 years ago
- Status changed from Resolved to Closed