【发布时间】:2015-12-26 11:25:42
【问题描述】:
我正在使用knockoutjs绑定数据,这是我第一次使用knockoutjs,我有一个列表数据显示到表格,当用户向下滚动到底部时,它将加载更多新数据。这是我的代码:
HTML:
<div data-bind="template: { name: 'product-template', foreach: listProduct }" id="data-list"></div>
<script type="text/html" id="product-template">
<table>
<tr>
<td data-bind="text:name"></td>
<td data-bind="text:description"></td>
</tr>
</table>
</script>
这里还有 JS:
var product = {
get : function(pageNumber){
var self = this;
self.listProduct = ko.observableArray([]);
request.product(pageNumber, function(resp){
//response list data of product
//example: {"data":[{"name":"sony","desciption":"this is sony"},{"name": "toshiba","description": "this is toshiba"}]};
if(pageNumber>1){
self.listProduct.push(resp.data);
}else{
self.listProduct(resp.data);
}
})
}
}
然后我这样调用函数:
ko.applyBindings(new product.get(1), document.getElementById("data-list"));// it's success to bind data
并在我调用向下滚动到底部事件时绑定更多数据:
ko.applyBindings(new product.get(2), document.getElementById("data-list"));// I got error: Error You cannot apply bindings multiple times to the same element
有什么问题吗?谢谢。
【问题讨论】:
标签: javascript jquery knockout.js