1

How to initialize an instance of the custom element declared in an entry page?

For example:

<!-- index.html -->
<polymer-element name='my-elm' attributes="count" noscript>
<template>
<span>{{count}}</span>
</template>
</polymer-element>

<my-elm id="my-elm"></my-elm>
// index.dart
(querySelector("#my-elm") as dynamic).count = 1;

When I attempt to initialize count propery of my-elm polymer element instance I get the following execption: Breaking on exception: Class 'HtmlElement' has no instance setter'count='. What is wrong in my code?

Roman
  • 2,145
  • 4
  • 26
  • 33
  • possible duplicate of [how to implement a main function in polymer apps](http://stackoverflow.com/questions/20982489/how-to-implement-a-main-function-in-polymer-apps) – Günter Zöchbauer Jul 04 '14 at 16:32
  • It seems that this is other issue.. I cteated the new issue https://code.google.com/p/dart/issues/detail?id=19840 – Roman Jul 04 '14 at 20:47

1 Answers1

2

EDIT

You can only access fields of the backing class this way.
If you don't have a class you can accesss it using

querySelector("#my-elm").attributes['count'] = '1'; // attributes can only store strings

EDIT END

You need to delay code in index.dart until Polymer is ready

import "package:polymer/polymer.dart";

main() {
  initPolymer().run(() {
    // code here works most of the time
    Polymer.onReady.then(() {     
      // some things must wait until onReady callback is called
      (querySelector("#my-elm") as dynamic).count = 1;
    });
  });
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Unfortunately, this doesn't work.. NoSuchMethodError: method not found: 'count=' – Roman Jul 04 '14 at 16:44
  • Do you have other idea? It seems that this is a bug of polymer-dart. – Roman Jul 04 '14 at 16:50
  • Check in the debugger what's returned by `querySelector('#my-elm')` – Günter Zöchbauer Jul 04 '14 at 18:33
  • the instance of HtmlElement class. I checked in the debugger. – Roman Jul 04 '14 at 20:44
  • 1
    I think the problem is the `noscript`. As far as I remember I run into this myself a while ago. It doesn't work to define attributes for such elements. In Polymer.dart elements without a class have quite a few limitations that similar Polymer.js elements don't have. – Günter Zöchbauer Jul 04 '14 at 20:56
  • Thanks, I will try and let you know – Roman Jul 05 '14 at 18:13
  • I tried.. Usage of 'attributes' collections works great if the count is string. the type of the attributes propery is Map. But I would like to data bind the custom element to the instance of a complex class.. It seems that this is not possible in the current version of Polymer.dart. So I will write the class for my custom element.. I don't want to do it, but I don't see any way to solve this very simple task :) I remain hopeful that this will be possible in a next version of Polymer.js. – Roman Jul 05 '14 at 20:36
  • This doesn't work in Polymer.dart and probably also won't in the future. – Günter Zöchbauer Jul 05 '14 at 22:01