在没有 JsProxy 的情况下使 Dart-Polymer 模型 class 可反射

Make Dart-Polymer model class reflectable without JsProxy

我在 AppEngine 上有一个 dart 服务器 运行 和一个 Dart-Polymer 客户端。这个想法是共享共同的逻辑,例如模型。但是,有一个小问题。通常你会让你的 model-类 扩展 JsProxy 而不是用 @reflectable 注释所有相关字段以便在数据绑定中使用它们。不幸的是,与 AppEngine 的数据存储一起工作的模型已经继承自模型。除此之外,我不确定 dart:js 是否在服务器上可用。因此,我必须找到另一种方法让我的模型可以反射 UI。

用@reflectable 注释 Project#name 无效。与输出相同的空 div。

在模型中包含 JsProxy 时:

The built-in library 'dart:html' is not available on the stand-alone VM.

有什么想法吗?

@Kind(name: 'Project', idType: IdType.Integer)
class Project extends Model {

  @StringProperty(required: true)
  String name;

}

@PolymerRegister('project-list')
class ProjectList extends PolymerElement {

  @property List<Project> projects;

  ProjectList.created() : super.created();

  ready() {
    fetchProjects();
  }
}

<dom-module id="projects-page">
  <template>
    <template is="dom-repeat" items="{{projects}}">
      <div>{{item.name}}</div>
    </template>
  </template>
</dom-module>

输出:

<div></div>

这是一个已知问题https://github.com/dart-lang/polymer-dart/issues/664

目前最好的解决方法是在应用注释并包装共享模型的地方使用代理对象 类。

class FooProxy extends JsProxy implements Foo {
  final Foo model;

  FooProxy(this.model);

  @reflectable
  int get bar => model.bar;
  set(int value) { model.bar = value}
}