Search code examples
dartdart-polymer

Register Polymer Class Name


So i've been trying to add a PolymerElement dynamically to an other PolymerElement.

 MyPolyElem newRow = new MyPolyElem();
  (newRow as MyPolyElem).type = type;  // -> Exception
  panelBody.append(newRow); 

Exception: Uncaught Error: type 'HtmlElement' is not a subtype of type 'MyPolyElem ' in type cast.

If I try to access type, it says that newRow is a HtmlElement and does not have "type". If I try to cast it, is says that its not a subtype of HtmlElement.

@CustomTag('poly-elem')
class MyPolyElem extends PolymerElement {
    @published String type = "int";

    void attached() {
        super.attached();
    }
    factory MyPolyElem() => document.createElement("poly-elem");
    MyPolyElem.created() : super.created() {}
}

Apparently, if i create a Polymer Element dynamically, its a HtmlElement. If i would write in the the HTML as tag and query for it, its a MyPolyElem. So i'd like to know how i could register the Name of the Class for this Tag.


Solution

  • Dynamically created elements can be cast like declaratively added elements.
    I'm pretty sure this doesn't work in your case because Polymer is not yet fully initialized.

    See how to implement a main function in polymer apps for how to initialize Polymer properly when you have a custom main() method.

    I never used document.createElement() to create a Polymer element dynamically. I used new Element.tag('xxx-yyy') instead. If the main() method is not the cause you might try this (see also Dynamically create polymer element)

    Even though you create it dynamically you need to add an import like when you add it decoratively.