reset 속성을 사용하여 선택 상자를 사용하여 단일보기에서 필터링 된 콜렉션을 렌더링하려고합니다. 첫 번째 문제는 선택 상자와 렌더링 된 내용이 모두 작동하도록 동일한 div에 나타나야한다는 것입니다. 이게 정상인가? 두 번째 문제는 필터링 된 결과 만 표시하는 대신 필터링 된 컬렉션을 렌더링 된 내용의 끝 부분에 추가하는 대신 교체합니다. 내 ._each 함수에 추가되는 것을 알고 있습니다. 필터링 된 항목을 추가하기 때문에 작동하지 않습니다. 전체 컬렉션의 끝.Backbone.js가 단일보기로 재설정 된 콜렉션을 렌더링합니다.
(function($) {
var images = [
{ tags: "Fun", date : "April 3, 2012", location : 'Home', caption : 'Having fun with my lady'},
{ tags: "Chillin", date : "April 4, 2012", location : 'Home', caption : 'At the park with my lady'},
{ tags: "Professional", date : "April 5, 2012", location : 'Home', caption : 'At the crib with my lady'},
{ tags: "Education", date : "April 6, 2012", location : 'Home', caption : 'Having fun with my baby'},
{ tags: "Home", date : "April 3, 2012", location : 'Home', caption : 'Having fun with my lady'},
{ tags: "Professional", date : "April 4, 2012", location : 'Home', caption : 'At the park with my lady'},
{ tags: "Fun", date : "April 5, 2012", location : 'Home', caption : 'At the crib with my lady'},
{ tags: "Chillin", date : "April 6, 2012", location : 'Home', caption : 'Having fun with my baby'},
{ tags: "Fun", date : "April 3, 2012", location : 'Home', caption : 'Having fun with my lady'},
{ tags: "Education", date : "April 4, 2012", location : 'Home', caption : 'At the park with my lady'},
{ tags: "Personal", date : "April 5, 2012", location : 'Home', caption : 'At the crib with my lady'},
{ tags: "Personal", date : "April 6, 2012", location : 'Home', caption : 'Having a play date'}
];
var Item = Backbone.Model.extend({
defaults : {
photo : 'http://placehold.it/200x250'
}
});
var Album = Backbone.Collection.extend({
model : Item
});
var ItemView = Backbone.View.extend({
el : $('.content'),
initialize : function() {
this.collection = new Album(images);
this.render();
$('#filter').append(this.createSelect());
this.on("change:filterType", this.filterByType);
this.collection.on('reset', this.render, this);
},
template : $('#img_container').text(),
render : function() {
var tmpl = _.template(this.template);
_.each(this.collection.models, function (item) {
this.$el.append(tmpl(item.toJSON()));
},this);
},
events: {
"change #filter select" : "setFilter"
},
getTypes: function() {
return _.uniq(this.collection.pluck("tags"), false, function (tags) {
return tags.toLowerCase();
});
},
createSelect: function() {
var filter = this.$el.find("#filter"),
select = $("<select/>", {
html: "<option>All</option>"
});
_.each(this.getTypes(), function (item) {
var option = $("<option/>", {
value: item.toLowerCase(),
text: item.toLowerCase()
}).appendTo(select);
});
return select;
},
setFilter: function (e) {
this.filterType = e.currentTarget.value;
this.trigger("change:filterType");
},
filterByType: function() {
if (this.filterType === "all") {
this.collection.reset(images);
} else {
this.collection.reset(images, { silent: true });
var filterType = this.filterType,
filtered = _.filter(this.collection.models, function (item) {
return item.get("tags").toLowerCase() === filterType;
});
this.collection.reset(filtered);
}
}
});
var i = new ItemView();
})(jQuery);
의 라인을 따라 뭔가! 쓰레기 한 무리! 나는 또한 당신이 제안한 것을 시도 할 것입니다. 그래서 내가 본 마스터보기를 사용하는 데 더 많은 시간이 걸린다. 다시 Thx! – Teeman
답이 귀하의 질문을 해결할 수 있다면 왼쪽에있는 체크 표시를 선택하여 수락을 고려하는 것이 좋습니다. – Jack