Clear panel and add component repetead in short time

Hi everyone,

I have link and when i click on it, it should clear my content_panel and add a component.
Everything is okay if i just click on it one time.
The problem is when i doubleclick it or click on it several time, the content_panel add & repeat the same component multiple time.
Yet, it’s supposed clear before Add.

Thanks for the help. :wink:

1 Like

This typically happens if your content has yet to load before the next press of the button so the action that follows is: the content is cleared but since the content has not loaded yet there is nothing to clear! So you end up with two forms.

I’ve spent ages debugging this one in the past.

I built this into the hash routing dependency so that it doesn’t happen there and the approach I took was to have a global object _current_form.

I changed the link click to something like this

def load_form(form_to_load):
    new_form = form_to_load()
    if new_form is Globals._current_form:
        # then it’s safe to add this component 
        self.content_panel.clear()
        self.content_panel.add_component(new_form)

I set this global variable as the first thing in the init method so that it is set prior to anything that might cause a slightly slow form load.

from . import Globals

class Form1(Form1Template):
    def __init__(self, **properties):
        Globals._current_form = self
        self.init_components(**properties)

Another approach I’ve taken in the past is to use a decorator to remove the link click event handler while the form is loading and then re-add the event after the form had loaded. But I liked this solution less.

Thanks for your response but I have another question.
How can we define this Globals._current_form?

this might be helpful Global Variables

or this solution taking the Module approach

1 Like

Thanks. It work for me