1

I've the below custom element, and I want to register it in the same file

custombtn.dart:

class SubmitFonix extends HtmlElement {

  String name;

  factory SubmitFonix([String name]) => (new Element.tag(tag) as SubmitFonix)
       ..name=name
      ;

  SubmitFonix.created() : super.created() {
  ...
}

currently, I'm registering the element in the main function, as this:

void main() {
  document.registerElement(SubmitFonix.tag, SubmitFonix);
 }

I wanted to add the registration line to the custombtn.dart file, instead of having it in the main() function, but if I could not!

in polymer.dart, I noticed they are using below statement:

@initMethod
upgradePaperFocusable() => registerDartType('paper-focusable', PaperFocusable);

and in other file they have:

const initMethod = const InitMethodAnnotation();

class InitMethodAnnotation {
 const InitMethodAnnotation();
}

so, tried using the same concept, and updated my custom element file to be as:

    class SubmitFonix extends HtmlElement {

  String name;

  factory SubmitFonix([String name]) => (new Element.tag(tag) as SubmitFonix)
       ..name=name
      ;

  SubmitFonix.created() : super.created() {
  ...
}
@initMethod
upgradeCustomBtn() => document.registerElement(SubmitFonix.tag, SubmitFonix);

at execution, I got this this error in the console:

Exception: type 'HtmlElement' is not a subtype of type 'SubmitFonix' in type cast.
submit_btn.dart:10
SubmitFonix.SubmitFonix.

submit_btn.dart:10 is: factory SubmitFonix([String name]) => (new Element.tag(tag) as SubmitFonix)

any thought?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203

1 Answers1

1

@initMethod is a Polymer annotation and you can only use it if you use Polymer and I assume you do not. If you move the registerElement away from main you have it to put to a function or method which is called directly or indirectly from main.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks @Gunter, your comment along with this: http://stackoverflow.com/questions/26821567/custom-annotation-metadata-in-dart-lang/26830363#26830363 and this: http://stackoverflow.com/questions/26826521/executing-bundle-of-functions-by-their-metadata-tag-in-dart-lang helped me solving the issue – Hasan A Yousef Nov 10 '14 at 07:03