Reputation: 24948
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'));
Upvotes: 1
Views: 569
Reputation: 657268
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!'));
}
}
Upvotes: 1
Reputation: 2547
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);
}
Upvotes: 0