Programmatically create polymer Elements in Dart

时间:2016-04-18 08:48:55

标签: dart polymer

Need add polymer paper-dropdown-menu in DOM. I try this code:

    makePapersElements() {
      List _items = new List();

      for (var i = 0; i < 13; i++) {
        PaperItem item = new dom.Element.tag('paper-item');
        item.text = i;

        _items.add(item);
      }

      return _items;
    }

And add nodes in PaperListbox then in PaperDropdownMenu:

    List<PaperItem> items = makePapersElements();

    var element = new dom.Element.tag('div');

    PaperDropdownMenu dropMenu = new PaperDropdownMenu();
    PaperListbox listBox = new dom.Element.tag('paper-listbox');


    dropMenu.append(listBox);
    listBox.nodes.addAll(items);

    element.nodes.add(dropMenu);

    $['example'].nodes.add(element);

It's not work currently:

enter image description here

怎么做?

更新:添加了Gist https://gist.github.com/Rasarts/a0b6710e234ec8b4aa37f90e4cd14839

2 个答案:

答案 0 :(得分:3)

您可以使用PaperDropDownMenu创建Paperlistboxnew Xxx(),不需要new dom.Element.tag('Xxx'),因为这些元素包含构造函数以方便已经完成

我想这个问题是因为你没有使用Polymer DOM API。另见https://github.com/dart-lang/polymer-dart/wiki/local-dom。只有当您启用完整的影子DOM(使用完全polyfill而不支持本机支持)时,您才需要使用此API。

makePapersElements() {
  List _items = new List();

  for (var i = 0; i < 13; i++) {
    PaperItem item = new PaperItem();
    item.text = i;

    _items.add(item);
  }

  return _items;
}
List<PaperItem> items = makePapersElements();

var element = new dom.Element.tag('div');

PaperDropdownMenu dropMenu = new PaperDropdownMenu();
PaperListbox listBox = new PaperListbox();


Polymer.dom(dropMenu).append(listBox);

// not sure this will work 
Polymer.dom(listBox).childNodes.addAll(items);

// alternatively
var listboxDom = Polymer.dom(listBox);
for(var item in items) {
  listboxDom.append(item);
}

Polymer.dom(this)appen(dropMenu);
// ro Polymer.dom(this.root)appen(dropMenu);


Polymer.dom($['example']).append(element);

答案 1 :(得分:1)