Я пытаюсь отобразить отфильтрованную коллекцию из одного представления, используя поле выбора, используя свойство сброса. Первая проблема заключается в том, что поле выбора и отображаемый контент должны отображаться в одном и том же div, чтобы работать. Это нормально? Вторая проблема заключается в том, что вместо отображения только отфильтрованных результатов он добавляет отфильтрованную коллекцию в конец отображаемого содержимого, а не заменяет его. конец всей коллекции. Вот мой пример кода ниже:
(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);