1

I have a custom button component done in Polymer Dart:

<div id="buttonDiv">
  <my-button id="traceButton"
   mode="icon" faicon="fa-comment-o"
   toolTip="Print a simple comment"
   disabled="false" on-click="{{ traceSomething }}">
  </my-button>
</div>

I'm trying to copy/paste this button somewhere else. So a user defines it somwhere, and I basically move it by way of getting $['buttonDiv'].children then inserting it somewhere else. The problem is that {{ traceSomething }} is now irrelevant since it's not part of the new parent. I get errors saying that the parent object, which is another polymer component doesn't have an instance getter "traceSomething".

My question is, is there a way to remove "traceSomething" before I insert it somwhere else? I tried removing the "onClick" event listeners, but the buttons still wants to call that function upon click. Also, I've tried adding a preventDefault, etc, like in: In Dart, if I listen to a click event with two listeners, how do I know which happens first? But, no luck.

Community
  • 1
  • 1
Alex L.
  • 416
  • 1
  • 4
  • 14

1 Answers1

1

I'm not sure what you mean by copy/past. Do you clone the element, or do you just append it to some other elements children.

Anyway, I don't think you can remove the event listener if it was added declaratively. If you add it imperatively it is easy to remove and readd later.

import 'dart:async';

...

StreamSubscription subsc;
@override
attached() {
  super.attached();
  subscr = onClick.listen((e) => (this.parentNode as ShadowRoot).host.traceSomething(e));
}

@override
detached() {
  super.detached();
  if(subscr != null) {
    subscr.cancel();
  }
}

See also https://stackoverflow.com/a/22168745/217408 about accessing the parent of a Polymer element (for Dart Polymer <= 0.16.x)

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Well, in my case, I want to get the Child elements from a div, and "move" them somewhere else.... Like: List origen = $['buttonDiv'].children; //Then origen.forEach((c) => $['newHome'].children.add(c)); – Alex L. Nov 20 '15 at 18:42