4

如果我不使用自定义元素,我无法将模型绑定到表格。自定义元素工作正常,但是当我尝试手动绑定模型而不创建自定义组件时,不会显示任何数据。有任何想法吗?

custom_table.html

<!DOCTYPE html>
<html>
  <body>    
    <polymer-element name="custom-table" extends="div" apply-author-styles>            
      <template>      
        <table>
          <tbody>
            <tr template repeat="{{people}}">
              <td>{{firstName}}</td>
              <td>{{lastName}}</td>
            </tr>
          </tbody>
        </table>
      </template>      
      <script type="application/dart" src="custom_table.dart"></script>
    </polymer-element>   
  </body>
</html>

custom_table.dart

import 'package:polymer/polymer.dart';

class Person  {
  String firstName;
  String lastName;        
  Person(this.firstName, this.lastName);
}

@CustomTag('custom-table')
class CustomTable extends PolymerElement with ObservableMixin {
  List people = [new Person('Bob', 'Smith'), new Person('Alice', 'Johnson')];
}

没有自定义元素版本:

聚合物测试.html

<!DOCTYPE html>
<head>        
    <script src="packages/polymer/boot.js"></script>
</head>
<html>
  <head>
    <meta charset="utf-8">
    <title>Polymer test</title>
    <link rel="stylesheet" href="polymer_test.css">
  </head>
  <body>   
   <h1> Table test </h1>
    <template id="tableTemplate" repeat>
      <table>       
        <tbody>
          <tr template repeat="{{ people }}">
            <td>{{firstName}}</td>
            <td>{{lastName}}</td>
          <tr>            
        </tbody>  
      </table>
    </template>    
    <script type="application/dart" src="polymer_test.dart"></script>    
  </body>
</html>

聚合物测试.dart

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

class Person {
  String firstName;
  String lastName;        
  Person(this.firstName, this.lastName);
}

class Message extends Object with ObservableMixin {  
  List<Person> people = toObservable([new Person('Bob', 'Smith'), new Person('Alice', 'Johnson')]);
}

main() {
  var msgModel = new Message();  
  TemplateElement template = query('#tableTemplate');
  template.bindingDelegate = new FancySyntax();
  template.model = msgModel;
}
4

3 回答 3

1

将其添加到 main 是否有帮助?

void main() {
  initPolymer([], () {
    // put existing main code in here
  });
}

polymer/boot.js 应该这样做,但我认为现在它不会启动,除非页面至少有一个聚合物元素(这是一个错误)。

<polymer-element>或者,您可以向 HTML添加一个虚拟对象。这将解决问题。

于 2013-08-14T19:15:10.340 回答
0

我的模板定义中缺少绑定属性。相反,我错误地使用了重复。它应该是:

<template id="tableTemplate" bind>

代替:

<template id="tableTemplate" repeat>
于 2013-09-10T12:57:02.307 回答
0

你能试一下吗

templateBind(query('#tableTemplate'))
    ..bindingDelegate = new FancySyntax()
    ..model = msgModel;

http://api.dartlang.org/docs/releases/latest/template_binding.html#templateBind

于 2013-11-07T05:41:18.343 回答