Project

General

Profile

loading js library after rendering widget

Added by egor bayanov over 5 years ago

Hi.

recently began to study Wt.

There is such a problem.

When the library loads the elements of the domain it needs to load before it is rendered. Is there a way to ensure that the js file is loaded after the widget is rendered?

file is connected using the following method: Wt::WApplication::instance()->require("file.js")

Thanks


Replies (4)

RE: loading js library after rendering widget - Added by lm at over 5 years ago

I'm just starting to look into running javascript from my Wt application. It looks like the section, "Invoking JavaScript or including scripts" at https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WApplication.html has everything you're wanting. It also points to javascript members of Wt::WWidget (https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WWidget.html).

For instance, in your file.js, you can define a function, foo. In the Wt::WWidget you mentioned, call Wt::WWidget::doJavaScript to call the function. In the documentation of Wt::WWidget::doJavaScript, it says, "This method guarantees that the JavaScript code is only run when the corresponding DOM node (using jsRef()) resolves to a valid DOM object." which sounds like what you want.

I haven't done any of this yet, I'm just relaying some documentation to you. But I am planning to implement something like this myself.

RE: loading js library after rendering widget - Added by egor bayanov over 5 years ago

Realized in this way (Although I still think that this is a bad decision)

void funcName(Wt::WWidget* widget, std::string path){
    std::string s;

    std::ifstream ifs(path);
    if(ifs.is_open())
    {
        s.assign((std::istreambuf_iterator<char>(ifs.rdbuf())), std::istreambuf_iterator<char>());
        std::cout << s;
        ifs.close();

        Wt::WApplication::instance()->declareJavaScriptFunction("foo", "function() {"+s+"}");

        widget->doJavaScript(Wt::WApplication::instance()->javaScriptClass()+".foo();");
    }
}

RE: loading js library after rendering widget - Added by lm at over 5 years ago

What do you not like about it? I'm glad you were able to make it happen; it adds hope to what I'm doing!

RE: loading js library after rendering widget - Added by Wim Dumon over 5 years ago

egor,

require() will cause the JS to be loaded synchronously, so yes, it will be loaded before doJavaScript() calls are executed.

Wim.

    (1-4/4)