Reputation: 4228
I'm learning Backbone and am wondering if in Backbone, when defining el
in our views, must el
be an element in the DOM
and not an element defined inside our template?
Upvotes: 0
Views: 61
Reputation: 18859
I don't believe that it really matters in essence and unless I'm missing something obvious, I don't understand how defining the el in the template would be safer.
I tend to define my el
as an existing DOM element that serves as a wrapper for the view template, but at times I also define it at runtime, depending on the situation.
The concept of el
en views
in general are related to Event bubbling
, which is functionality that is provided by the public DOM api.
Generally it means that events captured by inner elements, will be propagated to outer elements.
In terms of Backbone View objects, it means that when you define an el
, it serves as the parent element for the DOM section that is represented by the View. When you define events on its descendants in the View object, el will register the events that are captured and propagated by its child elements, unless you decide to prevent the event from bubbling, which can be achieved by event.stopPropagation()
.
In my conclusion, I think that you are flexible in how you use this theory and that the choice depends on the situation you are currently dealing with.
Anyhow, the question may be a bit malformed, since el must always be an element in the DOM before it can deal with events.
Upvotes: 1