【发布时间】:2014-07-24 17:44:49
【问题描述】:
我想用另一个自定义元素 (localized_element) 更新自定义元素 (signin_element) 中的属性 labels。但是属性 labels 在操作过程中已经将类型从 Map 更改为 String。
解释:
1/ 应用程序刚启动后,自定义元素(signin-element)使用 Map 更新属性 labels(参见文件 signin.dart)。
2/ 自定义元素 localized-element 初始化后,属性 labels 会使用来自外部 json 文件的新 Map 进行更新。方法更新无法将新 Map 分配给属性 labels,因为现在是 String。
铬返回警告:
signin-element 上的属性在 Polymer 之前是数据绑定的 升级元素。这可能会导致不正确的绑定类型。
localized-element 的属性在 Polymer 之前是数据绑定的 升级元素。这可能会导致不正确的绑定类型。
最后返回错误:
Class 'String' has no instance method '[]='.
Signin.html:
<core-animated-pages selectedindex="0" notap id="core_animated_pages">
<section id="section1" layout horizontal active center-justified center>
<core-card id="core_card" layout vertical>
<paper-input label="Your email addres" floatinglabel id="email_input"></paper-input>
<paper-input label="Your password" floatinglabel id="password_input"></paper-input>
<div id="div" layout horizontal end-justified>
<paper-fab icon="check" id="paper_fab"></paper-fab>
</div>
<h2>{{labels['hello']}}</h2>
</core-card>
</section>
</core_animated_pages>
<localized-element id="l10n" locale={{locale}} labels={{labels}}></localized-element>
signin.dart
@CustomTag('signin-element')
class Signin extends PolymerElement {
@published String locale = 'en';
@observable Map labels = toObservable({
'hello': 'Hello 1'
});
Signin.created() : super.created();
}
localized.dart
@CustomTag('localized-element')
class Localized extends PolymerElement {
@published String locale = 'en';
@published Map labels;
Localized.created() : super.created();
ready() {
super.ready();
_loadTranslations();
}
update() {
if (!_l10n.containsKey(locale)) return;
var l10n = _l10n[locale];
labels['hello'] = l10n['hello'];
}
List locales = ['en', 'fr'];
_loadTranslations() {
locales.forEach((l10n)=> _loadLocale(l10n));
}
Map _l10n = {};
_loadLocale(_l) {
HttpRequest.getString('i18n/translation_${_l}.json')
.then((res) {
_l10n[_l] = JSON.decode(res);
update();
})
.catchError((Error error) {
print(error.toString());
});
}
}
【问题讨论】:
标签: dart dart-polymer