聚合物1.0:铁页,不带子事件切换页
Polymer 1.0: iron-pages not switching pages with children events
在一个"iron-pages"组件中有一个父组件和两个子组件。当父进程从其中一个子进程接收到事件时,它应该切换页面。问题是,当事件到达父组件时,它执行代码来切换子组件,但什么也没有发生。但是,如果代码是在父组件本身上执行的,那么它就可以工作了。
我的问题是:当子组件发送事件时,为什么父组件无法切换页面?
下面是两个子组件的代码:// this is "child-comm.html"
<link rel="import"
href="bower_components/polymer/polymer.html">
<dom-module id="child-comm">
<style>
div {
width: 300px;
height: 300px;
background-color: #CCC;
}
</style>
<template>
<div>I'm child 0!</div>
</template>
</dom-module>
<script>
Polymer({
is: "child-comm",
listeners: {
'tap': 'doTap'
},
doTap: function() {
this.fire("taped-child")
}
});
</script>
和另一个子:
this is "child-comm2.html"
<link rel="import"
href="bower_components/polymer/polymer.html">
<dom-module id="child-comm2">
<style>
div {
width: 300px;
height: 300px;
background-color: #C00;
}
</style>
<template>
<div>I'm child 2!</div>
</template>
</dom-module>
<script>
Polymer({
is: "child-comm2",
listeners: {
'tap': 'doTap'
},
doTap: function() {
this.fire("taped-child")
}
});
</script>
然后我们有父组件:
<link rel="import"
href="bower_components/polymer/polymer.html">
<link rel="import"
href="child-comm.html">
<link rel="import"
href="child-comm2.html">
<link rel="import"
href="bower_components/iron-pages/iron-pages.html">
<dom-module is="parent-comm">
<template>
<iron-pages selected="0">
<child-comm on-taped-child="switchPages"></child-comm>
<child-comm2 on-taped-child="switchPages"></child-comm2>
</iron-pages>
</template>
</dom-module>
<script>
Polymer({
is: "parent-comm",
switchPages: function(e) {
this.pages.selectNext(); // this will not work if the event is created in a child component
console.log(this.pages); // this will print the correct node on console even when being fired by a child custom event
},
ready: function() {
this.pages = document.querySelector('iron-pages');
this.switchPages(); // this will switch pages correctly
console.log(this.pages);
}
});
</script>
谢谢…
解决这个特殊问题的一个快速方法是在父节点上拦截子事件,但不让它调用方法来切换页面,而是异步调用方法。下面是父节点的工作代码:
<dom-module is="parent-comm">
<template>
<iron-pages selected="0">
<child-comm on-taped-child="asyncSwitchPages"></child-comm>
<child-comm2 on-taped-child="asyncSwitchPages"></child-comm2>
</iron-pages>
</template>
</dom-module>
<script>
Polymer({
is: "parent-comm",
switchPages: function() {
this.pages.selectNext();
},
asyncSwitchPages: function(e) {
this.async(this.switchPages);
},
ready: function() {
this.pages = document.querySelector('iron-pages');
}
});
</script>
注:我很想听到"为什么"我必须这样做,所以我就不接受这个答案了
我认为您需要侦听自定义事件。尝试将下面的代码添加到parent的ready函数中。
this.addEventListener('taped-child', function(e) {
this.switchPages();
});
相关文章:
- 如何在分页事件中突出显示数据表中的单词
- 如何在 PHP 中使用 OnChange 事件的下拉列表中选择一个选项后,对大量数据行设置分页
- 如何从谷歌地图事件访问光标位置(即页面的x和y轴)
- 在加载多页模板Jquery Mobile的页面后运行事件
- 在视图被替换后,主干视图事件未激发,然后放回页面上
- ASPX页面上的onkeydown事件
- 我可以更改当点击剑道网格下一页按钮时触发的事件吗
- 区分剑道网格上一页/下一页点击与转到第一页/最后一页点击事件
- jQuery Mobile pageremove事件中留下的页面的Url/文件名
- 在第三次onPageClick事件后,使用twbspagination插件和AJAX更新JS分页
- 如何在Ibooks固定版式的书中只防止翻页事件
- WebView中的Html页不会激发'可见性更改'用户按下主页按钮时的事件(iOS 8)
- EXTJS 3,如何手动控制分页事件
- jQuery 移动页面显示事件未在第一页上触发
- 换页时的Phonegap白页
- 重定向到一个操作点击事件的页面的锚
- 我不能换页
- 从用户控件事件调用页面上 ASP.Net javascript
- 聚合物1.0:铁页,不带子事件切换页
- 如何在换页后继续功能