Calling B4J methods directly
There are more than one ways to skin a cat, it is said, so yesterday I made an assumption and it turned out to be true. I needed to call a b4j method that was no linked to an event handler in my class module, turns out, I can.
Application Name: ATM
B4J Class Name: pgIndex
B4J Method To Call: DeleteMember
Problem Statement: This does not have an event handler
My Scenario: When I click a delete button, the user should be prompted with a confirm dialog (this uses sweet dialog), when the user selects Yes, a B4J method should be fired to actually delete the record else No. The generated Yes / No button for the sweetmodal are automatically generated and they dont have IDs to I cannot assign an event handler, however you can pass javascript to execute onYes and onNo. So I need a B4J method to be executed and not javascript, this B4J method anyway will be transpiled to BANano javascript subroutine.
The aha moment!
Solution: pass
_banano_atm_pgindex.deletemember(); to the Yes script of sweetmodal. Yes, you can define your own buttons for the sweetmodal but I didnt want to add buttons, yet.
Below is my DeleteMember sub routine in B4J in my pgIndex class, this class is like a 'page' for my BANano project.
user clicked yes on confirmation, process the deletion
Sub DeleteMember
'get the memberid to delete
Dim memberid As String = Page.GetLocalStorage("memberid")
'get the existing members
Dim strMembers As String = Page.GetLocalStorage("members")
If strMembers = "" Then
Else
'this will store all unmatching records
Dim nList As List
nList.Initialize
'convert existing members to a list
Dim lstM As List = App.Json2List(strMembers)
'each member is a json string
Dim mTot As Int = lstM.Size - 1
Dim mCnt As Int
For mCnt = 0 To mTot
Dim strMember As String = lstM.Get(mCnt)
Dim memberM As Map = App.Json2Map(strMember)
'the id number is the key
Dim sidnumber As String = memberM.Get("idnumber")
If sidnumber <> memberid Then
nList.Add(strMember)
End If
Next
'convert the list and save it back
strMembers = App.List2Json(nList)
Page.setlocalstorage("members", strMembers)
'create the table
CreateTable
End If
End Sub
This code below, which traps the eventid, when a deleterow_ linked element is clicked, a sweetmodal prompt is displayed for the user to confirm, when they click 'Yes', the code to delete the member is executed.
Case "deleterow"
Dim rowPos As String = App.mvfield(thisEvent,2,"_")
Dim rowKey As String = App.MvField(thisEvent,3,"_")
'ask the user
'save this memberid for later retrieval
Page.SetLocalStorage("memberid", rowKey) 'BANano.setLocalStorage
'save the table row for later retrieval
Page.SetLocalStorage("index", rowPos)
'call a method existing in this application
'_banano_atm_pgindex.deletemember();
Dim onYes As String = Page.CallMethod(App.AppName,moduleName,"deletemember();")
'code to generate the sweet modal confirm script (this returns javascript code only)
Dim msg As String = Page.SweetModalConfirm($"Confirm Delete: ${rowKey}"$, _
"Are you sure that you want to delete this record? You will not be able to undo your changes.", "_banano_atm_pgindex.deletemember();", "")
'same as banano.eval(javascript)
Page.Execute(msg)
#ExploringBANano
Update: 30 Jan 2019
A new method has been added to BANano as BANano.CallSub