2017-08-16 66 views
1

我正在使用REST API来获取Polymer 2.0.2项目中的一组对象。响应是这样的:将新的JSON对象追加到Polymer中的数组中

[ 
    {"name":"John","city":"Mumbai"}, 
    {"name":"Ron","city":"New York"}, 
    {"name":"Harry","city":"Lisbon"} 
] 

当接收到响应,设置我的属性命名content如下:

_contentAjaxResponseHandler(event) {   
    this.set('content', event.detail.response);  
} 

这工作只要REST API被调用一次。 现在,我想在用户滚动到页面底部并将其添加到现有数据时获取下一批。

所以,我的问题是,什么是最新的方式追加新的结果到现有的content数组?换句话说,聚合物中2个阵列合并的最佳方式是什么?

直到现在,我能想到的唯一的办法就是循环在新的结果,并呼吁push method.Something这样的:

_contentAjaxResponseHandler(event) { 
    let newResponse = event.detail.response; 
    newResponse.forEach(function(newObj){ 
     this.push('content',newObj); 
    }); 
} 
+0

您是否正在尝试实施像“iron-scroller”这样的功能? https://www.webcomponents.org/element/PolymerElements/iron-scroll-threshold – Ofisora

+0

嗨感谢您的回复。我试过这个组件。此组件已损坏,无法正常工作。其Github页面上存在未解决的问题,特别是以下问题:https://github.com/PolymerElements/iron-scroll-threshold/issues/23。由于这个组件不起作用,我开始编写我自己的滚动处理程序 –

+0

您是否尝试过使用'concat'函数? https://www.w3schools.com/jsreF/jsref_concat_array.asp – Ofisora

回答

1

下面的代码为我工作:

_contentAjaxResponseHandler(event) { 
    let newResponse = event.detail.response; 
    this.set('content',this.content.concat(newResponse)); 
}