Failed: widgets attached to parent in turn back and forth

Two simple layouts are attached in turn back and forth to the parent layout successfully.
https://snippet.dhtmlx.com/0042rbhd

But a bit more complicated ones failed. I’ve spent one whole day digging on it but not out. Please help. thx!
https://snippet.dhtmlx.com/uz4q54uh

Please, try to use the following solution:
https://snippet.dhtmlx.com/48jkxgg5
the logic is:

  1. Click on a button (click event)
  2. Wait till the click process performs (awaitRedraw())
  3. Attach a new view (attach from the awaitRedraw)

This problem occurs because you are performing a click action for the component which you are detaching at that moment.

It works perfectly, thanks sematik.