R
R
roman99662016-01-11 17:46:45
JavaScript
roman9966, 2016-01-11 17:46:45

How to get element($el) from backbone collection element?

I iterate over the collection of models using _.each, I need to remove certain elements from the DOM. Each element (model) of the collection corresponds to a certain type. How to get this view and remove it from the DOM?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Дмитрий Кравченко, 2016-01-11
@roman9966

можно подписать вьюху на кастомное событие модели

var MyModel = Backbone.Model.extend({
  initialize: function(){
    //create MyView with {model:this}
  }
});
var MyView = Backbone.View.extend({
  initialize: function(options){
    //your code
    this.model.on('customEventName', this.remove);
  }
});

var MyCollection = Backbone.Collection.extend({
  model: MyModel
});


//go

var items = new MyCollection(arrayOfItems);

items.each(function(item){
  if(item.get('someParam'))
    item.trigger('customEventName');
})

в принципе, можно сделать вьюху параметром модели, но я считаю, что модель не должна вообще ничего ни о каких вьюхах знать

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question