Setting id and className dynamically in Backbone.js views

后端 未结 8 1819
生来不讨喜
生来不讨喜 2021-01-29 19:24

I am in process of learning and using Backbone.js.

I have an Item model and a corresponding Item view. Each model instance has item_class and item_id attributes, that I

8条回答
  •  余生分开走
    2021-01-29 20:08

    The other examples are not showing how to actually grab the data from the model. To dynamically add id and class from the model's data:

    var ItemView = Backbone.View.extend({
       tagName:  "div",
    
       render: function() {
         this.id = this.model.get('item_id');
         this.class = this.model.get('item_class');
         $(this.el).attr('id',this.id).addClass(this.class).html('Some Stuff'); 
       }       
    });
    

提交回复
热议问题