How to avoid template wrapping with empty div in Backbone?

前端 未结 6 2000
死守一世寂寞
死守一世寂寞 2020-12-11 15:13

When I create view backbone creates empty div-container if el is not set. Template (this.$el.html(this.template(this.model.toJSON()))) inserted in that div. How

6条回答
  •  旧巷少年郎
    2020-12-11 15:46

    You can render the view into any container you like either specifying the $el property yourself or using the setElement() method before calling render:

    var jobView = new JobView({
      model:jobModel
    });          
    
    jobView.setElement($('#your_selector_here'));
    
    console.log(jobView.el);
    

    If you look at the docs for View.el you'll see that you can also specify the el property either when writing your view, or by passing a parameter in the constructor.

提交回复
热议问题