OnbeforeTaskAdded invoked twice in the second time gantt


When I have the second gantt component the OnbeforeTaskAdded gets invoked twice. In order to reproduce the problem here is the sample code (attached) and the steps:

  • Click + in the Project #1 and create element G2. Alert message is shown once.
  • Click capacity chart.
  • Click + in the Project #1 and create element G2. Alert message is shown twice.

index.html.zip (1.85 KB)

I also tried the detachallEvents before and after the init, and didn’t work either.

While working with standard edition of gantt you have only one gantt object. So when you are calling the load_gantt second time - it doesn’t create a new gantt , it operates with same gantt objects. And second call to attachEvent adds second hander - so both old and new one will be called.

detachAllEvents call must resolve problem with event handlers, but I don’t see that it was used in the attached code.

Also, you can consider usage of PRO edition, which allows to hanve a multiple independent gantts on the same page.

After uploading my sample file, I added the detachAllEvents before the init and didn’t work either.

On the other hand, I’m using only one gantt in the page, and not two. Due to this, I can still use the normal edition.

The provided sample is to replicate the scenario in my application, but in a simplify environment for you to replicate.

attached a new sample code for an Angular app started from the angular seed project.

Steps to reproduce the problem are:

  • Create a task by clicking the + button.
  • An alert message is shown.
  • Click view2
  • Now click again the + button at top level
  • Click save. The alert message is shown twice.

GanttViewerAngularApp.zip (101 KB)

detachAllEvents remove all event handlers that been added to the gantt (this part seems to work). Although, gantt uses some of the events for inner logic. Detaching all of them disables some functionality of the component, which makes a method quite useless.

Probably you could use detachEvent, to remove handlers by their ids
docs.dhtmlx.com/gantt/api__gantt … event.html

As for the issue itself, as Stanislav said, gantt.init does not create a new object. The method is mostly designed to move gantt to different html container. So all added event handlers and modified configs are expected to stay after calling gantt.init

In your case, you can isolate part of the code that should be executed only once (all of gantt.attachEvent ) and make sure it is triggered only when application starts

as a suggestion, I would change the init method name for a more meaning name.