B4J Question [BANano]: [SOLVED] How to CallSub inside #If JavaScriptSmart? - 2

serro efe

Member
Licensed User
Longtime User
Hi
@Mashiane's question
It was asked and resolved here. It gives an error in my tests.
jquery 2.1.0 attached to the project
Console Error; Uncaught SyntaxError: expected expression, got '%'
App.js Error;
JavaScript:
function banano_dummies_page1() {var _B;this._ws=null;this.__2={};this.callok=function()
....
...
}

   function TestFunc(){   

    (function() {if (typeof _B[(%"callok"%).toLowerCase()]==="function") {var CSr = _B[(%"callok"%).toLowerCase()](,_B);if(CSr!=null) {return CSr}}})()       

         //_B.__2

        //alert("TEST");

}




I'm testing it like @alwaysbusy does in post 2. My codes are as follows. I can use TestFunc () from within Javascript.
Code:
Sub Process_Globals
    Private BANano As BANano  'ignore
    Private ws As BANanoWebSocket
    Private MethodLoad As Object
End Sub


Sub CallOk
    Log("demo 121212121")
End Sub
         
'//${MethodLoad}      
'//${BANano.CallSub(Me, "CallOk", Null)}
#if JavascriptSmart
    function TestFunc(){        
         ${MethodLoad}
        //alert("TEST");
}
    #End If

public Sub Show  
    MethodLoad = BANano.CallBack(Me, "CallOk", Null)
    ....
    ....
End Sub

console error; Uncaught ReferenceError: _B is not defined
JavaScript:
function banano_dummies_page1() {var _B;this._ws=null;this.__2={};this.CallOk=function()
....
...
}

function TestFunc(){         

         _B.__2  --> Error

        //alert("TEST");

}
Is there a way to call BANano function from within javascript.
 

alwaysbusy

Expert
Licensed User
Longtime User
Some things have changed since then and that doesn't work anymore it seems.

I did some tests and this is how it should be done. (just for a test, I added a parameter num in this example to demonstrate passing parameters)
The key difference is you have to add _B as the first parameter of your JavaScript method, and when calling it using the BANano.RunJavascriptMethod(), passing Me as the first parameter. And don't forget to add (); ,or (num); in this case, in the JavaScript!
B4X:
...
function TestFunc(_B, num){
...
BANano.RunJavascriptMethod("TestFunc", Array(Me,123456789))
...

Full code:
B4X:
Dim MethodLoad as BANanoObject
...

Sub CallOk(number As Int)
    Log("demo " & number)
End Sub

#if JavascriptSmart
function TestFunc(_B, num){  // Make sure this JavaScript method has _B as first parameter.  You can add others, like num in this example after it.      
    ${MethodLoad}(num);      // Make sure you add '(...);' after the method name! (can be without params '();'    
}
#End If

' HERE STARTS YOUR APP
Sub BANano_Ready()
    ' define an int that we will pass to the CallOk method
    Dim num As Int
    ' defining the definition of CallOk
    MethodLoad = BANano.CallBack(Me, "CallOk", Array(num))
    ' somewhere in you program running TestFunc with as first parameter the class that has TestFunc (in this case Me),
    ' and as second parameter e.g. a number
    BANano.RunJavascriptMethod("TestFunc", Array(Me,123456789))
     ...

Alwaysbusy
 
Last edited:
Upvote 0

serro efe

Member
Licensed User
Longtime User
Thank you for the answer. It works as it should.
When I want to run from external javascript file;
I get the value of Me at every page entry to a variable like the one below.
B4X:
BANano.RunJavascriptMethod ("b4j_Ws_Me", Array (Me, Null))
I run it from javascript file like this.
JavaScript:
function b4j_Ws_Me (b_, data) {
  window.console.log (b_);
  window.console.log (data);
  TestFunc (b_, data);
}
Thanks Again
 
Upvote 0
Top