1

In JavaScript, we can add listener to the custom element as mentioned here by:

proto.createdCallback = function() {
   this.addEventListener('click', function(e) {
   alert('Thanks!');
  });
};

I tried making equivalent DART code as:

    Element launchElement(){
       this.onClick.listen((e)=>print('Thanks!'));
       return (shadow);  
    }

Am I doing something wrong here?

the full code of my custom element is:

class MegaButton extends ButtonElement  {
  static final tag = 'mega-button';
  factory MegaButton()=>new Element.tag('button', tag);

MegaButton.created() : super.created() {
  var shadow = this.createShadowRoot();
  shadow.text='save'; 

  Element launchElement(){
    this.onClick.listen((e)=>print('Thanks!'));
    return (shadow);  
  }

}

registered it as:

document.registerElement(MegaButton.tag, MegaButton, extendsTag: 'button');

and called it by:

myDiv.nodes.add(new Element.tag('button', 'mega-button'));
1
  • What is launchElement for? You define an inner method but never call it. Commented Oct 12, 2014 at 12:49

2 Answers 2

1
class MegaButton extends ButtonElement {
  static final tag = 'mega-button';

  factory MegaButton()=> new Element.tag('button', tag);

  MegaButton.created() : super.created() {
    var shadow = this.createShadowRoot();
    shadow.text = 'save';
  }

  void attached() {
    this.onClick.listen((e)=>print('Thanks!'));
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can add the listener in this way:

class MegaButton extends ButtonElement {
  static final tag = 'mega-button';
  factory MegaButton() => new Element.tag('button', tag);

  MegaButton.created() : super.created() {
    var shadow = this.createShadowRoot();
    shadow.text = 'save';

    Element launchElement() {
      return (shadow);
    }

  }

}

void main() {

  document.registerElement(MegaButton.tag, MegaButton, extendsTag: 'button');

  DivElement myDiv = querySelector("#mydiv");
  MegaButton mbutton = new Element.tag('button', 'mega-button');
  mbutton.onClick.listen((e) => print('Thanks!'));
  myDiv.nodes.add(mbutton);


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.