B4J Question [BANano] Error using BANanoObject.Selector.RunMethod ?

Toky Olivier

Active Member
Licensed User
Longtime User
Hello,
Can you tell me please the difference between the two operations belows?
This one:
JavaScript:
    #If javascript
        $(".dropdown").on("click", ".dropdown-item", function(){
            _B[("HandleDropdownItemClicks").toLowerCase()](event,_B);
        });
    #End If

And this one:
B4X:
Dim jq As BANanoObject
    jq.Initialize("$")
    jq.Selector(".dropdown").RunMethod("on", Array("click", ".dropdown-item", BANano.CallBack(Me, "HandleDropdownItemClicks", Null)))

The thing is, the first one works but not the second one... It's for event Delegation.
 
Solution
The thing is, the first one works but not the second one... It's for event Delegation.
you should pass the event parameter:

B4X:
Dim jQ As BANanoObject
Dim event As BANanoEvent
jQ.Initialize("$")
jQ.Selector(".dropdown").RunMethod("on", Array("click", ".dropdown-item", BANano.CallBack(Me, "HandleDropdownItemClicks", Array(event))))

Sub HandleDropdownItemClicks(event As BANanoEvent)
    Log(event.CurrentTarget)  
End Sub

Of course the element with '.dropdown' (the parent) should exist for delegates to work.

Without jquery (using Umbrella JS, which does not have such a system with their 'on' method) , you can do this:
B4X:
Dim DropdownItemClicks As BANanoElement
Dim event As BANanoEvent...

Toky Olivier

Active Member
Licensed User
Longtime User
The second code is transpiled like by the transpiler:
JavaScript:
_jq=null;
_jq=$;
_jq(".dropdown")["on"]("click",".dropdown-item",function() {
    if (typeof _B[("HandleDropdownItemClicks").toLowerCase()]==="function") {
        return _B[("HandleDropdownItemClicks").toLowerCase()](_B)
    }
});
But it doesn't work.
 
Upvote 0

Toky Olivier

Active Member
Licensed User
Longtime User
Something like:
B4X:
    Dim DropdownItemClicks As BANanoElement
    DropdownItemClicks.Initialize("#" & mName & "_dropdown_menu")
    DropdownItemClicks.On("click", Me, "HandleDropdownItemClicks")
works but the element should exists before calling ".On" method. Me I would like to add the event even for not yet created elements.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
The thing is, the first one works but not the second one... It's for event Delegation.
you should pass the event parameter:

B4X:
Dim jQ As BANanoObject
Dim event As BANanoEvent
jQ.Initialize("$")
jQ.Selector(".dropdown").RunMethod("on", Array("click", ".dropdown-item", BANano.CallBack(Me, "HandleDropdownItemClicks", Array(event))))

Sub HandleDropdownItemClicks(event As BANanoEvent)
    Log(event.CurrentTarget)  
End Sub

Of course the element with '.dropdown' (the parent) should exist for delegates to work.

Without jquery (using Umbrella JS, which does not have such a system with their 'on' method) , you can do this:
B4X:
Dim DropdownItemClicks As BANanoElement
Dim event As BANanoEvent
DropdownItemClicks.Initialize(".dropdown")
DropdownItemClicks.AddEventListener("click",BANano.CallBack(Me, "HandleDropdownItemClicks", Array(event)),True)

Sub HandleDropdownItemClicks(event As BANanoEvent)
    Dim eventTarget As BANanoElement
    eventTarget.Initialize(event.Target)
    Dim clickedElement() As BANanoElement = eventTarget.Closest(".dropdown-item")
    If clickedElement.Length > 0 Then
        Log(clickedElement(0).GetAttr("id"))
        ' Note: because of a chain bug, you may have to do it like this for now until next release      
        ' Dim tmpElem As BANanoElement = clickedElement(0)
        ' Log(tmpElem.GetAttr("id"))
    End If  
End Sub

Alwaysbusy
 
Last edited:
Upvote 0
Solution
Top