Pregunta

I was fiddling with the "new" instructions for polymer dart 0.10.0-pre.10 only to realize I had package 0.9.5 installed (on an updated Dart Editor). And could only get code to run using main() => dostuff(); Adding component1 as per instructions just broke whatever worked.

I set pubspec.yaml polymer dependency to >= 0.9.9 and it auto pub gets the version 0.10.0-pre.10. Then I made changes as suggested and moved dostuff() to a custom element class (extends PolymerElement) and put @initMethod above it. It does not run.

And as I got it to run before I was unable to find a way to bind new items from a JSON file (which I successfuly got through http) to the polymer element.

mylist.dart

import 'package:polymer/polymer.dart';
import 'dart:html';

@CustomTag('my-list')
class MyListElement extends PolymerElement {

  @observable List mylist = ['one', 'two', 'three'];

  @initMethod
  static dostuff() {
    print("initMethod");
    // get json and pass to mylist
  }
}

mylist.html

<polymer-element name="my-list">
  <template>
    <ul>
      <template repeat="{{item in mylist}}">
        <li>{{item}}</li>
      </template>
    </ul>
  </template>
  <script type="application/dart;component=1" src="mylist.dart"></script>
</polymer-element>

index.html

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sample app</title>
    <link rel="import" href="packages/polymer/polymer.html">
    <link rel="import" href="mylist.html">
    <script src="packages/browser/dart.js"></script>
  </head>
  <body>
    <h1>MyList</h1>

    <div id="container1">
      <json-list id="my-list1"></json-list>
    </div>
  </body>
</html>
¿Fue útil?

Solución

This code (especially @initMethod) inside the element doesn't make sense. @CustomTag('my-list') does this already. You need @initMethod() only when you want a method executed like main() that is outside of an Polymer element.

You can put this code inside the constructor of MyListElement or better inside polymerCreated before the super call.

import 'package:polymer/polymer.dart';
import 'dart:html';

@CustomTag('my-list')
class MyListElement extends PolymerElement {

  @observable List mylist = ['one', 'two', 'three'];

  @override
  polymerCreated() {
    print("initMethod");
    // get json and pass to mylist
    super.polymerCreated();
  }
}

You didn't get Polymer 0.10.0-pre.10 because it is a pre-release which is indicated by the - after the patch version (not by pre).
Pub by default ignores pre-releases. You have to enforce them by a version constraint like '>=0.10.0-'

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top