Use JS variables directly

Hi, I need to change a few properties of the 'mainWindow' in main.js on runtime.I try to use it in a 'asm' block, but an error message 'mainWindow is not defined' is shown.

For the same reason, I want to import some JS file into my electron project and use some global variables and objects in it, can I use them directly?If not, how can I use external JS code in my TMS Web Core electron project?

If there is a way to use JS variables and objects in a TMS Web Core electron project directly , it will be very very helpful.

Best Regards

Hi,

By mainWindow you mean in an Electron project from the main.js file? If that is the case, mainWindow is not exposed as a global variable and it should stay that way for security reasons. You can however try to send messages through IPC to your main.js.
Example on this messaging mechanism from your previous question.

Normally if you have a JS file that you include in your project.html and that JS file has global variables/objects, then you can use those global variables/objects from an asm block. Do you experience issues with this?

Hi,
have you define your mainWindow unit in the uses.
For me it's not fun and not easy but I use this trick :slight_smile:

Here is to add and manage marker on GoogleMap

unit GMapTools;

interface

uses
System.SysUtils, System.Classes, JS, Web, db,
WEBLib.Dialogs,WEBLib.JSON,WEBLib.JsonData, WEBLib.CDS,
WEBLib.WebSocketClient, WEBLib.REST, JSONDataset,

// API
MainPage;

implementation

uses
JsTools,
OtherTools;

{-----------------------------------------------------------------------------
procedure onMapMarkerClick (Be careful in this case place never this function before implementation)
-----------------------------------------------------------------------------}
procedure onMapMarkerClick(pMarker : TJSObject;..........);
var
MyVar : string;
begin
// MainForm is the mainoject of MainPage unit
MainForm.UseAfunction();
end;

{-----------------------------------------------------------------------------
procedure CreateOneMarker
-----------------------------------------------------------------------------}
procedure CreateOneMarker(......);
begin
asm
if (gMap) // gMap is a global javascript var declared in MyglobalJSVar.js file
{
var MyCurMarker = createtheMarker(......); // createtheMarker is a javascript function call from here
if (MyCurMarker)
{
MyCurMarker.addListener("click", () => { $impl.onMapMarkerClick(MyCurMarker, ......) }); // onMapMarkerClick just upon this function
}
}
end;
end;

Thanks @Piazzon_Gilles and @Tunde , I will try your solution and report back later.

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.