Project

General

Profile

Access JavaScript variable in Wt?

Added by Osman Zakir about 7 years ago

I have this code: https://gist.github.com/DragonOsman/5475d9f9b6fc8c379eba77a504b71bcf . I need to know how to access the variable called "location" that have in the JavaScript code in Wt. I want to reproduce this JavaScript code in Wt: https://gist.github.com/DragonOsman/c6e8fb15343544e662f474c5a526d1c2 (except I'd like a good way to make the country-currency-map module (which is of course a Node.js module I've downloaded the files and other dependencies for) accessible in client-side JavaScript; it'd be good if there's a way to do that in Wt - is there?) in Wt.

What I want to do is to open an info window on the map at the location I get using HTML5 geolocation and have an HTML form appear on that info window. On that form should be an input element for taking the money amount, two dropdown menus (one for the "to" currency and one for the "from" currency), a way to show the result of the conversion calculation, and a submit button. The form submits to the C backend code using XMLHttpRequest. The dropdown menus are populated with currencies by also making an XMLHttpRequest to a currency API to get a list of currencies. The currency API doesn't allow changing the base currency in their free plan, so I also want to set the "from" currency dropdown to the default currency (USD) and then disable it.

It'd be appreciated if the people on the Wt IRC who can help me out (by that I mean anyone good with JavaScript and how to access variables in JavaScript code in Wt) also reply.


Replies (12)

RE: Access JavaScript variable in Wt? - Added by Osman Zakir about 7 years ago

If I can use a WJavaScriptHandle for this, then I'd like to know how to get a valid handle. The documentation doesn't seem to say, as it only seems to talk about invalid handles. I need more information.

RE: Access JavaScript variable in Wt? - Added by Roel Standaert about 7 years ago

No, WJavaScriptHandle is only for interactive WPaintedWidget features.

What you could use is a JSignal. Take a look at the JavaScript example in examples/javascript in the Wt source code.

RE: Access JavaScript variable in Wt? - Added by Osman Zakir about 7 years ago

Thanks for the reply.

Along with that, could you also please help me with understanding how to use JSignal (especially if just looking at the example isn't enough for me). Given what I have now, how do I access the variable location in the JavaScript code in Wt? Do I have to start over and use just JSignal this time to do it?

RE: Access JavaScript variable in Wt? - Added by Osman Zakir about 7 years ago

I still need some help. Gist link for my code is the same as before; I've just updated the code in it. I want to know if what I have so far for the first call to doJavaScript() and the createCall() code is alright.

More information about how to use jsRef() to get the object google.maps.Map in C would also be appreciated. And with what I have right now, is it enough to be able to access a variable called position in the C code? Since I passed a string "position" in as the second argument to the JSignal constructor.

If you're busy right now, I'm sorry for bothering you. Just, please reply ASAP. Thanks in advance.

RE: Access JavaScript variable in Wt? - Added by Osman Zakir about 7 years ago

Thanks.

By the way, if you wrote an actual function called updatePosition, why not use it on line 45 there? Why did you use a lambda instead? Is there a special reason? If you needed to capture the variable map in the lambda, why write updatePosition in the first place?

RE: Access JavaScript variable in Wt? - Added by Osman Zakir about 7 years ago

Also, I need help with porting the rest of the JavaScript from line 17 onward in that script code on the Gist to Wt as well (except for line 49).

RE: Access JavaScript variable in Wt? - Added by Roel Standaert about 7 years ago

Oh, I just forgot to remove that updatePosition. You could do it either way.

RE: Access JavaScript variable in Wt? - Added by Osman Zakir about 7 years ago

@Roel Janssen: I need a fallback for when HTML5 geolocation isn't available. The fallback would be opening an info window centered on the initial coordinates (in this case (--34.397, 150.644)), with the "to" currency dropdown menu set to the currency of that location. What would be the best way to do this here? The update_postion.connect lambda should have a condition checking for the availability of HTML5 geolocation, and if it's not available, it should center the map and info window on the initial coordinates. How do you propose I do that? Thanks in advance for any help or advice. And I also want to do like on this page: https://www.w3schools.com/howto/howto_js_alert.asp to show the result of the calculation, and I want to make it show when a response comes from the server for the conversion calculation request.

Also, how should I do lines 34 through 81 on my JavaScript code file on that Gist in Wt?

RE: Access JavaScript variable in Wt? - Added by Osman Zakir about 7 years ago

Also, I need a loop in there for adding option elements for each currency. Please tell me how add that as well.

RE: Access JavaScript variable in Wt? - Added by Osman Zakir about 7 years ago

Another question: Can WTemplate be used to to inject a loop in the middle of HTML for a form?

RE: Access JavaScript variable in Wt? - Added by Osman Zakir about 7 years ago

I'm going to back to just using WTemplate here and using those and

tags for linking in external CSS and Javascript. I'll try to get help on how to do that and get it to work.

My updated C code Gist: https://gist.github.com/DragonOsman/5475d9f9b6fc8c379eba77a504b71bcf , and the Javascript code Gist: https://gist.github.com/DragonOsman/c6e8fb15343544e662f474c5a526d1c2 . If there's anyone familiar enough Javascript, I'd also like to ask if what I have in form_submit and on_submit will be enough for what to do when receiving and trying to display the answer from the server for the POST request from the currency conversion form. Also, how do I handle that POST request in my C code and how do I know that the button for submitting the form was pressed? Can I hook up a JSignal to the element with type "submit" created in my Javascript code while it's in that separate file? Would I need to for this?

I'm also not sure if I should have that convert() function in the class derived from WApplication. Would it better to have that outside the class but just call it when handling the POST request from the form?

Since I read that a WTemplate is a XHTML template, I also wrote an actual XHTML file. The code in it is the same as what I've put as my argument to the WTemplate constructor. I'd like to know if I can just link the WTemplate instance with the actual file.

    (1-12/12)