【发布时间】:2015-08-03 07:56:52
【问题描述】:
我有一个称为 parent-page 的父聚合物元素和一个称为 child-page 的子元素。
parent-page 调用子页面并传递一个数组。例如,在父页面中:
<child-page items={{itemsArray}}></child-page>
现在,基于某些活动,子页面会触发一个带有新数组的事件。
例如,在子页面中:
this.fire('eventPerformed', newArray);
父页面正在侦听此数组并收到预期值。 现在,我想将该新数组传递给子页面,以便根据新数组呈现子页面。
如何实现?
编辑:我的子页面如下所示。
<dom-module id="child-page">
<style>
</style>
<template>
<template is="dom-repeat" items="{{itemArray}}" as="fooditem">
<div class="horizontal layout">
<div>{{fooditem.quantity}}</div>
<div>{{fooditem.name}}</div>
</div>
</template>
<paper-button on-click"changeArray"> ChangeArray</paper-button>
</template>
<script type="text/javascript">
Polymer({
is:'child-page',
properties:{
itemArray:Array
},
changeArray:function(){
this.itemArray=<<Some new Array>>
this.fire('eventPerformed',newArray);
}
});
</script>
</dom-module>
有什么方法可以在同一个子页面中使用新数组调用模板重复?还是我必须向父页面触发事件并再次调用子页面?如何实现?
【问题讨论】:
-
但是子页面已经有数据了,不是吗?
-
好吧,你的意思是,我不应该触发这个事件?那么如何根据更改后的数组重新渲染相同的页面呢?
-
如果你有数据 (
newArray) 为什么不简单地更新你的属性? -
是的,有点奇怪你会向父级触发一个事件,只是让它重新通知更新数组的子页面。子页面已经知道这一点。如果您希望 dom-repeat 重新渲染,请尝试:
this.slice('itemArray', itemArray);
标签: javascript polymer web-component polymer-1.0