검색 기능을 통해 검색하는 json 개체의 커다란 컬렉션이 있지만 검색 문자열에 따라 출력이 달라질 수 있습니다. 명부. 모바일 환경에서 터치 모브를 추가하고 각 객체에 터치 시작 및 터치를 추가하면 번거롭고 메모리 소비가 발생합니다. backbone.js를 사용하여 객체를 표시하는 최소한의 방법이 있으며 버튼과 같은 트리거를 사용하면 강력해질 수 있다는 솔루션을 발견했습니다. 나는 그걸 가지고 앞으로 나아갈 방법을 모르지만. 이것은 버튼이없는 작동 예제입니다. 그리고 내가 어떻게 소리를 지르는거야? 내가 당신을 잘 이해하면단추를 사용하여 json에서 백본 컬렉션을 채우는 방법
<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>
모델 데이터가 서버 [fetch] (http://backbonejs.org/#Collection-fetch) – mumu2
에서 반환되면 this.render()가 this.collection.fetch()를 호출하여 "재설정"이벤트가 트리거됩니다. 맞습니다 ... 나는 그것을 감독했다. –
예, 해결책은 fetch()와 함께 이벤트 리스너를 갖는 것이고, fetch는 json에서 데이터를 검색하는 작업을 수행합니다. 감사 – syarul