我有大量的 json 对象集合,我通过搜索函数检索它们,虽然取决于搜索字符串,但输出可以达到我填充到列表中的数千个数组。在移动环境中,一旦我将 touchmove、touchstart 和 touchend 添加到每个对象,这就会变得很麻烦并且会消耗内存。我找到了解决这个问题的方法,即使用backbone.js 显示对象的最小方式,并且使用诸如按钮之类的触发器,这可能会变得健壮。虽然我不知道如何继续它。这是没有按钮的工作示例。我应该怎么做?
<script>
//model - define value objects.
var Client = Backbone.Model.extend({
defaults: {
name: 'cole',
age: '12'
}
});
//collection - load json
var ClientCollection = Backbone.Collection.extend({
defaults: {
model: Client
},
model: Client,
url: './json/test.json',
//override parse due to json format. point to "items"
parse: function (response, xhr) {
return response.items;
}
});
//view. init collection. listen for data to be loaded. render.
var ClientView = Backbone.View.extend({
initialize: function () {
this.collection = new ClientCollection();
this.collection.bind("reset", this.render, this);
this.collection.fetch();
},
render: function () {
//append to html here ...
//alert(this.collection.at(0).get("name"));
//alert(this.collection.length)
for (var i = 0; i < this.collection.length; i++) {
$('#append-el').append('<li>' + this.collection.at([i]).get("name") + '; ' + this.collection.at([i]).get("age") + '</li>')
}
}
});
var clientView = new ClientView();
</script>
<div id = "append-el"></div>