B4J Library [BANanoReef] Create reactive, state based components & UI

Discussion in 'B4J Libraries & Classes' started by Mashiane, Aug 4, 2019.

  1. Mashiane

    Mashiane Expert Licensed User



    So I took a small break to check this out, out of curiosity. Reef does one thing, renders the UI and only updates the stuff that has changed.

    To develop webistes / web apps with BANano, one uses html, css and javascript. Here we go to basics and create a reactive HTML component, a h1 using data-properties.

    We will update parts of it using a template and setting its data properties. What we have below is a HTML h1 component that gets updated every 3 seconds to update the name property..


    1. We create a reef object.
    2. We set the data to it
    3. We create a template for our HTML element and pass it the data
    4. We render the reef to the body of the page.


    Let's take a loot at the code...

    We have defined a process globals reef component.

    'initialize a reef and set the object to render it to
    'define the properties for the element, we have greeting and name
    'define the h1 html element
        reef.SetTemplate($"<h1>#greeting#, #name#!</h1>"$)
    'we render the element to the body
    We want to change the name at each 3 seconds..

    We then define a timer..

    nameCnt = 0
    timer.Enabled = True
    When the timer fires, we change the name property and refresh the element.

    Sub timer1_tick
        nameCnt = nameCnt + 
    Select Case nameCnt
    Case 1
    Case 2
    Case 3
    Case 4
            nameCnt = 
    End Select
    End Sub
    jmon, Kiffi and joulongleu like this.
  2. Mashiane

    Mashiane Expert Licensed User

    In this next example, we create a button named btn1, we give it a blue background and text of white. We also make it to have a width and height of 100px. This is just normal html and css coding. We place it inside a div called buttons1. This replaces everything inside that div.


    1. Lets create 2 divs, 1 to render the greetings in post 1 and another to hold the button.

    Dim cont1 As UOEHTML
    'create a div with id buttons1
        Dim buttons1 As UOEHTML
        'get the body of the page, created by banano,
        Dim body As BANanoElement = banano.GetElement("#body")
    'empty the body
        'add cont1 and buttons1 to body
    2. Lets create a button element and add an event to it.

    'lets create another html5 component and render it to buttons1 container
        Dim btn1 As BANanoReef
    "button").SetID("btn1").SetLabel("Button 1")
        banano.GetElement("#btn1").HandleEvents("click", Me, "btn1_click")
    As the button is an internal reef component (updated reef class to handle these types of definitions), its template will be rendered like this.

    <button id='btn1' style='background-color:blue; color:white; height:100px; width:100px;' >{label}</button>
    This means that we will be able to change the 'text' or {label} portion of the button.

    3. Let's trap the click event

    Sub btn1_click
    "I clicked btn1!")
    End Sub
  3. Mashiane

    Mashiane Expert Licensed User

    In this next example, we demo how to use a lagoon. A lagoon is used to store data that you can set in a re-active manner.

    Hi Universe!

    We will create a html element having...

    <h1  >{greeting}</h1><div id='todos'  ></div>
    We will use data from a lagoon to set the greeting by attaching the lagoon to another reef.

    1. We create a div (you can use any framework here off course to make-up your components)

    Dim lst As UOEHTML
    2. We add the div to the body..

    3. We create a lagoon just to store data, this does not have a template. Note that we initialize it with Null as it wont render anywhere

    "greeting""Hello, world!")
    "todos"Array("Buy milk""Bake a birthday cake""Go apple picking"))
    Here we have used .SetDataKeyValue for the data of the lagoon, still .SetData is appropriate but we wanted to simply this.

    4. We create the main Reef that will use the lagoon data to display what we need. This has a template and we attach it to the previous lagoon reef.

    "<h1>{greeting}</h1><div id='todos' ></div>")
    We then attach the sourceOfTruth reef to the app Reef with .SetAttachTo

    5. Now, lets reactively update the greeting to show Hi Universe!

    sourceOfTruth.Refresh(CreateMap("greeting""Hi, universe"))
  4. Mashiane

    Mashiane Expert Licensed User

    The last part of this, we render a list as part of the examples provided...


    Here is the code

    1. We get the data from the source of truth and get the saved to do list
    2. We build up the list structure that we need and set this to the template
    3. We attach the sourceoftruth and the app reefs.

    'render the list
        Dim tdm As Map = sourceOfTruth.GetData
    Dim tlist As List = tdm.Get("todos")
    Dim li As StringBuilder
    For Each strl As String In tlist
    $"<h2>Todo List</h2><ul>${li.tostring}</ul>"$)
    That's all folks...
    Don Oso likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice