我将Sample Polymer Tutorial App转换为Dart.Polymer。应用教程在这里[https://www.polymer-project.org/0.5/docs/start/tutorial/intro.html]
我面临的问题是纸质标签事件监听。聚合物的js版本是:
<core-header-panel>
<core-toolbar>
<paper-tabs id="tabs" selected="all" self-end>
<paper-tab name="all">All</paper-tab>
<paper-tab name="favorites">Favorites</paper-tab>
</paper-tabs>
</core-toolbar>
<div class="container" layout vertical center>
<post-list show="all"></post-list>
</div>
</core-header-panel>
<script>
var tabs = document.querySelector('paper-tabs');
var list = document.querySelector('post-list');
tabs.addEventListener('core-select', function() {
list.show = tabs.selected;
});
</script>
这是索引文件。我的Dart版本看起来像这样:
<core-header-panel>
<core-toolbar>
<paper-tabs id="tabs" selected="all" self-end>
<paper-tab name="all">All</paper-tab>
<paper-tab name="favorites">Favorites</paper-tab>
</paper-tabs>
</core-toolbar>
<div class="container" layout vertical center>
<post-list show="all"></post-list>
</div>
</core-header-panel>
<script type="application/dart">export 'package:polymer/init.dart';</script>
我如何收听纸质标签核心选择事件以更新脚本标签下的Dart中的标签内容?我尝试了这个但是没有用。
var tabs = document.querySelector('paper-tabs');
var list = document.querySelector('post-list');
tabs.addEventListener('core-select', (e) {
list.show = tabs.selected;
});
答案 0 :(得分:1)
现在最简单的方法是在函数上使用@whenPolymerReady注释。
<script type="application/dart">
import 'package:polymer/polymer.html';
export 'package:polymer/init.dart';
@whenPolymerReady
void startup() {
/// Custom code here, polymer is now initialized.
var tabs = document.querySelector('paper-tabs');
var list = document.querySelector('post-list');
tabs.on['core-select'].listen((_) {
list.show = tabs.selected;
});
}
</script>
答案 1 :(得分:0)
<强> app_element.html 强>
imports ...
<polymer-element name="app-element>
<template>
<core-header-panel>
<core-toolbar>
<paper-tabs id="tabs" selected="all" self-end>
<paper-tab name="all">All</paper-tab>
<paper-tab name="favorites">Favorites</paper-tab>
</paper-tabs>
</core-toolbar>
<div class="container" layout vertical center>
<post-list show="all"></post-list>
</div>
</core-header-panel>
</template>
<script type="application/dart" src="app_element.dart"></script>
</polymer-element>
<强> app_element.dart 强>
@CustomTag('app-element')
class AppElement extends PolymerElement {
AppElement.created() : super.created();
ready() {
var tabs = shadowRoot.querySelector('paper-tabs');
// or shorter
// var tabs = $['tabs'];
// because it has an id and is statically added
var list = shadowRoot.querySelector('post-list');
tabs.addEventListener('core-select', (e) {
list.show = tabs.selected;
});
}
}
<强>的index.html 强>
<html>
<head>
<link rel="import" href="app_element.html">
</head>
<body>
<app-element></app-element>
<script type="application/dart">export 'package:polymer/init.dart'; </script>
</body>
</html>
但不是强制添加事件处理程序,而是以声明方式执行(使用Polymer的首选方法)
<强> app_element.html 强>
imports ...
<polymer-element name="app-element>
<template>
<core-header-panel>
<core-toolbar>
<paper-tabs id="tabs" selected="all" self-end
on-core-select="{{coreSelectHandler}}">
<paper-tab name="all">All</paper-tab>
<paper-tab name="favorites">Favorites</paper-tab>
</paper-tabs>
</core-toolbar>
<div class="container" layout vertical center>
<post-list show="all"></post-list>
</div>
</core-header-panel>
</template>
<script type="application/dart" src="app_element.dart"></script>
</polymer-element>
<强> app_element.dart 强>
@CustomTag('app-element')
class AppElement extends PolymerElement {
AppElement.created() : super.created();
coreSelectHandler(Event e) {
list.show = tabs.selected;
}
ready() {
var list = shadowRoot.querySelector('post-list');
...
}
}