Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is use of tagName, id, and className properties in Backbone View? While we can access dom element with el

Why do the properties tagName, id and className exist in a Backbone View?

like image 549
ali asad Avatar asked Apr 23 '13 14:04

ali asad


2 Answers

Those properties are used if your view has to create its own element, that is, if it doesn't have a el attribute when instantiated (various reasons, I can go further in the matter). So you'll have a new element with the id id, classes className and attributes attributes.

You can find the relevant piece of code here. This _ensureElement method is used in the view's constructor.

like image 169
Loamhoof Avatar answered Sep 28 '22 07:09

Loamhoof


All Backbone views have an el property Read doc here. If you do not pass an el while instantiating a view, it will create an empty DIV and use it.

  • Now, just say you do not want to use DIV as the container to render your view. You want it to be a UL instead. Just specify the tagName property for your view and it will be used instead.

  • If you want to add some css classes to your container, use className.

  • If you want to add some attributes to it (For example you want to add data-* attributes to your el) use the attributes property of Backbone view.

like image 37
Sachin Avatar answered Sep 28 '22 07:09

Sachin