Создал динамически на странцие много itemView. Как логически понять сколько view открыто (itemView) и закрыть их?
define([
"app",
"text!templates/masterhome/homepage.html",
"collections/ItemhomeCollection",
"views/masterhome/homepage/itemhomeView",
"parsley"], function(app, homepageTemplate, ItemhomeCollection, ItemhomeView){
var homepageView = Backbone.View.extend({
tagName: 'div',
className: 'main_window',
template: _.template(homepageTemplate),
events: {
},
// Инициализация homepage.
// 1. Загружаем глобальную коллекцию ItemhomeCollection
// 2. ItemhomeCollection.fetch().
initialize: function () {
_.bindAll(this);
app.collectionItemhome = new ItemhomeCollection();
app.collectionItemhome.fetch({
reset:true,
success: function() {
if(DEBUG) console.log('COLLECTION ITEMHOME:: ' + _.size(app.collectionItemhome) )
}
});
if (_.size(app.collectionItemhome) > 0) this.renderAll();
this.listenTo(app.collectionItemhome, 'add', this.renderOne);
this.listenTo(app.collectionItemhome, 'reset', this.renderAll);
},
renderOne: function(item) {
this.itemhomeView = new ItemhomeView({ model:item })
if (item.get('status') == 1 || item.get('status') == 2) $('.mh-active-items').append(this.itemhomeView.render().$el);
if (item.get('status') == 3 || item.get('status') == 4) $('.mh-old-items').append(this.itemhomeView.render().$el);
return this;
},
renderAll: function() {
app.collectionItemhome.each(function(item){
this.renderOne(item);
}, this);
},
render:function () {
this.$el.html(this.template({}));
return this;
},
remove: function() {
this.el.remove();
this.stopListening();
return this;
}
//
При инициализации я загружаю коллекцию и делаю fetch()
Так как в опциях fetch я указал reset:true, срабатывает метод renderAll котороый циклично прогоняет коллекцию и вызывает для каждой модели коллекции метод renderOne()
В методе renderOne создается переменная this.itemhomeView как экземпляр вида ItemhomeView.
Так как основных видов у меня несколько. При закрытии этого - основного мне нужно закрыть все созданные ItemhomeView. Как я могу это сделать?
Если в методе remove я вызываю this.itemhomeView.remove();
То закрываю только последний созданный ItemhomeView