B4J Question [WebApp] B4J Websocket: Call a JS-Function that is not in the window-scope?

Kiffi

Well-Known Member
Licensed User
Longtime User
Hello,

calling a JS-Function from a B4J-Websocket-Server, that is in the window-scope (with ws.RunFunction("myTest", Args)) works like a charm:
B4X:
$(function () {
    $(document).ready(function () {

      [...]
     
    });
});

function myTest() {
  // no problem here
};

but i don't know how to call a JS-Function, that is not in the window-scope:
B4X:
$(function () {
    $(document).ready(function () {
       
        [...]

        function myTest() {
           // i don't know how to call this function
           // i get 'undefined is not a function'
        };

    });
});

because b4j_runFunction() wants to apply a function in the window-scope:

B4X:
function b4j_runFunction(func, params) {
    return window[func].apply(null, params);
}

Any ideas how to solve it?

Thanks in advance & Greetings ... Kiffi
 

warwound

Expert
Licensed User
Longtime User
I'm not a user of jquery (that is jquery syntax i see you using?) but can you try something like this:

B4X:
window.myTest=function(){
  alert('Hello World');
}

Martin.

(Edited to correct function name)
 
Upvote 0

Kiffi

Well-Known Member
Licensed User
Longtime User
Hello warwound,
hello Erel,

thanks for your fast replies!

@warwound: your solution works but it is unfortunately impractical for me because then the function is not accesible from the other 'inner' functions.

@Erel: Thanks for the background informations!

Perhaps i use b4j_eval()...

Thanks again & Greetings ... Kiffi
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
@warwound: your solution works but it is unfortunately impractical for me because then the function is not accesible from the other 'inner' functions.

You could declare all functions locally and then create global references to any of these (local) function(s) that require global scope:

B4X:
function myTest(){
  alert('Hello World');
}

function doSomething(){
  console.log('Hello World');
}

window.myTest=myTest;

Martin.
 
Upvote 0

Kiffi

Well-Known Member
Licensed User
Longtime User
warwound said:
You could declare all functions locally and then create global references to any of these (local) function(s) that require global scope: [...]
Perfect! Works like a charm. Thank you very much! :)

Erel said:
What is it needed for at all?
i have written a tool that converts PureBasic to JavaScript (PB2Web). And now i want to implement an interface to connect a B4J-Websocket-Server. The first steps are very promising. :)

Greetings ... Kiffi
 
Upvote 0

Kiffi

Well-Known Member
Licensed User
Longtime User
Hello Erel,

this is, what i'm doing now:
B4X:
$(function () {
  $(document).ready(function () {
    [...]

    function myTest() {
      [...]
    });

    window.myTest = myTest;

  });
});

and now i'm able to access the function with:
B4X:
ws.RunFunction("myTest", Args)

Thanks again & Greetings ... Peter
 
Upvote 0
Top