What is use of tagName, id, and className properties in Backbone View? While we can access dom element with el
Solution 1
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.
Solution 2
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 aUL
instead. Just specify thetagName
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 theattributes
property of Backbone view.
ali asad
Updated on January 20, 2020Comments
-
ali asad over 4 years
Why do the properties
tagName
,id
andclassName
exist in a Backbone View? -
Mbrevda almost 10 yearsAs per the docs,
attributes
is used to createthis.el
: "this.el is created from the view's tagName, className, id and attributes properties, if specified". So you can also setattributes.class
. It is NOT specifically fordata-*
attributes, although it can be used for that, as well as anything settable viajQuery.attr()
-
Alexander Mills almost 9 yearshow do we render the custom element into the desired location in the DOM?
-
Alexander Mills almost 9 yearshow do we render the custom element into the desired location in the DOM?
-
Rick almost 5 years@AlexanderMills Just like you would add any other html into the DOM. Something like
$("#container").html(myView.$el);