Setting id and className dynamically in Backbone.js views

后端 未结 8 1818
生来不讨喜
生来不讨喜 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:09

    Try to assign the values in initialize method this will directly assign id and class to the div attribute dynamically.

    var ItemView = Backbone.View.extend( {
        tagName : "div",   
        id      : '',
        class   : '',
    
        initialize : function( options ) {
            if ( ! _.isUndefined( options ) ) {
                this.id = options.item_id;
                this.class= options.item_class;
            }
        },
    
        render : function() {
            $( this.el ).html( this.template( "stuff goes here" ) ); 
        }
    } );
    

提交回复
热议问题