Прикрепленный обратный вызов во время children.clear в отсоединенном обратном вызове родителя

Простой сценарий: есть 2 веб-компонента, дочерний внутри родительского. При повторной вставке parent в какое-то другое место, обратный вызов detached был вызван для parent, и в нем вызывает child.clear() (возможно, это глупое место здесь). При очистке детей я вижу, что вызывается не только дочерний отдельный cb, но и прикрепленный:

https://dartpad.dartlang.org/26921310fb7f85dee672

import 'dart:html';

void main() {

  document.registerElement("x-my", MyElement);
  document.registerElement("x-ch", MyChild);
  var container = new Element.tag("x-my");
  document.body.children.add( container );
  print("creation done");

  // just re-add container to somewhere else
  var anothercontainer = new Element.div();
  document.body.children.add( anothercontainer );
  anothercontainer.children.add(container);

}

class MyElement extends HtmlElement {
  MyElement.created() : super.created();

  @override
  attached() {
    print("attached enter ${this.outerHtml}");
    super.attached();
    children.add( new Element.tag("x-ch") );
    print("attached leave ${this.outerHtml}");
  }

   @override
  detached() {
    print("detached enter ${this.outerHtml}");
    children.clear();
    super.detached();
    print("detached leave ${this.outerHtml}");
  }
}

class MyChild extends HtmlElement {
  MyChild.created() : super.created() { this.innerHtml = "child created"; }

  @override
  attached() {
    print("attached enter ${this.outerHtml}");
    super.attached();
    print("attached leave ${this.outerHtml}");
  }

   @override
  detached() {
    print("detached enter ${this.outerHtml}");
    super.detached();
    print("detached leave ${this.outerHtml}");
  }
}

Дает вывод:

attached enter <x-my></x-my>
attached enter <x-ch>child created</x-ch>
attached leave <x-ch>child created</x-ch>
attached leave <x-my><x-ch>child created</x-ch></x-my>
creation done
detached enter <x-my><x-ch>child created</x-ch></x-my>
detached enter <x-ch>child created</x-ch>
detached leave <x-ch>child created</x-ch>
attached enter <x-ch>child created</x-ch> <---- my question
attached leave <x-ch>child created</x-ch>
detached enter <x-ch>child created</x-ch>
detached leave <x-ch>child created</x-ch> <---- ends here
detached leave <x-my></x-my>
attached enter <x-my></x-my>
attached enter <x-ch>child created</x-ch>
attached leave <x-ch>child created</x-ch>
attached leave <x-my><x-ch>child created</x-ch></x-my>

person Vaulter    schedule 20.02.2016    source источник
comment
такое поведение в Chrome 48.0.2564.97.   -  person Vaulter    schedule 21.02.2016


Ответы (1)


Существует открытый вопрос об этом AFAIR. Я думаю, это вызвано изменением DOM из конструктора пользовательского элемента.

Попробуйте двигаться

 this.innerHtml = "child created";

to attached()

person Günter Zöchbauer    schedule 21.02.2016
comment
но ctor не вызывается при повторном присоединении элемента, не так ли? - person Vaulter; 22.02.2016