Dart 数据绑定结构化对象不会在我的聚合元素上显示任何更改
我正在将应用程序从0.5迁移到Polymer 1,并使用Dart。我做得不错,但现在我面临一些数据绑定问题: 我创建了一个聚合元素,通过编程在其局部DOM上插入另一个聚合元素,提供触点信息。第二个元素将从构造函数获取信息,并通过数据绑定将其显示在接口上 父省道:Dart 数据绑定结构化对象不会在我的聚合元素上显示任何更改,dart,polymer,polymer-1.0,dart-polymer,Dart,Polymer,Polymer 1.0,Dart Polymer,我正在将应用程序从0.5迁移到Polymer 1,并使用Dart。我做得不错,但现在我面临一些数据绑定问题: 我创建了一个聚合元素,通过编程在其局部DOM上插入另一个聚合元素,提供触点信息。第二个元素将从构造函数获取信息,并通过数据绑定将其显示在接口上 父省道: @PolymerRegister('test-app') class TestApp extends PolymerElement { TestApp.created() : super.created(); attach
@PolymerRegister('test-app')
class TestApp extends PolymerElement {
TestApp.created() : super.created();
attached() {
super.attached();
async(() {
insertTile();
});
}
void insertTile() {
String contactJSON = '{"contactId":"1", "firstName":"Lex", "surname":"Luthor"}';
Contact contact = new Contact(JSON.decode(contactJSON));
ContactTile tile = new ContactTile(contact, this);
Polymer.dom(this.root).append(tile);
}
}
父html:
<dom-module id="test-app">
<style>
:host {
display: block;
}
</style>
<template>
</template>
</dom-module>
儿童html:
<dom-module id="contact-tile">
<style>
:host {
display: block;
}
</style>
<template>
<div>{{ contact.contactId }}</div>
<div>{{ contact.firstName }}</div>
<div>{{ contact.surname }}</div>
</template>
</dom-module>
由于某些原因,在构造函数上更新联系人后,数据绑定不会在web界面上显示联系人的任何属性。有人能帮我吗?我已经在聚合物0.5上多次这样做,但在聚合物1上这不起作用
多谢各位
==============================
解决方案
该问题与ContactTile的构造函数上未发出通知有关。我可以通过使用更新属性并通知的“set”函数修改联系人来修复此问题。有两种方法可以做到这一点:
a@属性(通知:true)
b。polymerement.set(“名称”,值)
同样为了访问对象属性,我必须使类扩展JsProxy,并将@property添加到每个属性(或方法的@reflectable)
由于
Contact
类不是一个完整的自定义元素,因此它应该继承自JsProxy
并具有属性注释。这将使属性可以在html中访问。如下所示
class Contact extends JsProxy {
@property String contactId;
@property String firstName;
@property String surname;
Contact(Map map) {
contactId = map['contactId'];
firstName = map['firstName'];
surname = map['surname'];
}
}
你说的很有道理,尽管我已经测试过了,但由于某些原因,它不起作用。我真的不知道发生了什么。您是否尝试过使用带有硬编码值的联系人进行调试?如果这样做有效,那么我猜在
insertTile
准备就绪后,会丢失一个通知。通知是关键!在Polymer 0.5中,我可以修改属性,它会自动发出通知。在Polymer 1中,似乎要更改属性并启动它,我需要使用函数“set”。我将用解决方案更新我的问题。非常感谢你!此外,按照您的建议扩展JsProxy也是至关重要的。再次感谢!伟大的不容易弄明白,前一段时间我钻研了这个表单的在线代码。没有找到这方面的文档,所以有相关问题。
class Contact {
String contactId;
String firstName;
String surname;
Contact(Map map) {
contactId = map['contactId'];
firstName = map['firstName'];
surname = map['surname'];
}
}
@PolymerRegister('contact-tile')
class ContactTile extends PolymerElement {
factory ContactTile(Contact contact, Element parent) {
ContactTile tile = new Element.tag('contact-tile');
tile.contact = contact;
return tile;
}
ContactTile.created() : super.created();
@Property(notify: true)
Contact contact;
}
@PolymerRegister('contact-tile')
class ContactTile extends PolymerElement {
factory ContactTile(Contact contact, Element parent) {
ContactTile tile = new Element.tag('contact-tile');
// tile.contact = contact;
tile.set("contact", contact);
return tile;
}
ContactTile.created() : super.created();
@property
Contact contact;
}
import 'package:polymer/polymer.dart';
class Contact extends JsProxy {
@property
String contactId;
@property
String firstName;
@property
String surname;
Contact(Map map) {
contactId = map['contactId'];
firstName = map['firstName'];
surname = map['surname'];
}
}
class Contact extends JsProxy {
@property String contactId;
@property String firstName;
@property String surname;
Contact(Map map) {
contactId = map['contactId'];
firstName = map['firstName'];
surname = map['surname'];
}
}