B4J Tutorial [ABMaterial] B4JS - 02 Core Functions

Discussion in 'B4J Tutorials' started by alwaysbusy, Mar 6, 2018.

  1. alwaysbusy

    alwaysbusy Well-Known Member Licensed User

    See for a B4JS introduction: https://www.b4x.com/android/forum/threads/abmaterial-b4js-0-9.90249/

    Note: for this tutorial, I may use some typical ABM things. You can ignore them as they are just to show the result of the code in this tutorial.
    -------------------------------------------------------------------------------

    I can be short here: all the Core functions in B4J that are mentioned in the Introduction can be used. The ones not mentioned, not :rolleyes:

    However, I want to expand a little on some topics.

    Smart String Literal:
    This is such an extraordinary B4X feature that I definitely wanted at least some kind of support for it.

    An example:
    Code:
    Sub Process_Globals
       
    Dim myString As String
       
    Dim myGlobalName As String = "GlobalAlain"
    End Sub

    'Initializes the object. You can NOT add parameters.
    Public Sub InitializeB4JS()
       
    Dim myName As String = "Alain"

       
    ' smartstrings (does not support date, time or xml function
       myString = $"Hello planet "B4X"! This is a test
        from ${
    myName} and ${myGlobalName}"$
       
       
    Log("myString.Contains('planet'): " & myString.Contains("planet"))
    End Sub
    The result in the browsers log:
    Code:
    Hello planet "B4X"! This is a test
        from Alain 
    and GlobalAlain
    myString.Contains(
    'planet'): true
    As you can see, it can handle quotes in the string, and variables. It does not support the special $DateTime{}, $Date{}, $Time{} and $xml{} tags. But you can make a workaround for it by putting it in a variable first:

    Code:
    Dim myDate as String = DateTime.Date(DateTime.Now)
    Dim myTime as String = DateTime.Time(DateTime.Now)

    Dim myString as String = $"Here is the current date:  ${myDate} and the current time: ${myTime}"$
    The result in the console is something like this:
    Code:
    Here is the current date:  03/06/2018 and the current time: 15:37:47
    Timer:
    When using a timer, you must keep in mind that it runs in the browser. This means, even when your server is down, your timer will keep running.

    The usage is exactly as the B4J counterpart:
    Code:
    Sub Class_Globals
       
    Private lblTimer As Timer

       
    Private Page As ABMPage 'ignore, just to be able to run ABMPage functions
       Private ToastID As Int
       
    Private ABM As ABMaterial 'ignore, just to be able to use ABM constants
    End Sub

    ' Initializes the object. You can NOT add parameters and MUST be called InitializeB4JS.
    ' is automatically called when the class is created in Javascript
    Public Sub InitializeB4JS()
       lblTimer.Initialize(
    "lblTimer"5000)   
    End Sub

    Sub lblTimer_Tick
           ToastID = ToastID + 
    1
           
    Dim Show As String = cont1.B4JSVisibility
           
    If Show = "" Then
               Show = 
    "shown"
           
    End If
           
    Page.B4JSShowToast("TimerToast" & ToastID, "green""I'm shown in the timer without the server every 5 seconds! Label is: " & Show, 3000False)
    End Sub
    StringBuilder:
    Although the Smart String Literal is still better to use, B4JS does also supprt the StringBuilder.

    Code:
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append(
    "0123456789").Append(CRLF).Append("0123456789")
    Log(sb.ToString)
    Log("Length: " & sb.Length)  ' should show 21, including the return
    sb.Insert(2,"X")
    Log(sb.ToString)
    sb.Remove(
    2,3)
    Log(sb.ToString)
    The result output in the Browser console:
    Code:
    0123456789
    0123456789
    Length: 
    21
    01X23456789
    0123456789
    0123456789
    0123456789
    This concludes this tutorial. As a B4X programmer, there are not many differences with the familiar B4X syntax, which makes it very easy to get started with.

    Alwaysbusy
     
    magi6162, maXim, MichalK73 and 4 others like this.
  2. joulongleu

    joulongleu Member

    Hi:alwaysbusy: I test B4JSTutorial02 ,when I click Calculate button RunCode can't run,as follows:
    upload_2018-4-14_11-53-38.png
     
  3. joulongleu

    joulongleu Member

    sorry The result output in the Browser console is ok
     
Loading...