I have model Post and collection Posts. And want to make form with list of all post in <select id="multi" multiple="multiple">
. So i have to make a PostView render inside my #multi with just this template:
<option value=""><%= title %></option>
But finally I get it wrapped with div. Is there any solution for not wrapping this template with <div>
?
If you don't define an
el
(or tagName) for the view (in the class or during instantiation) the view will be placed inside a div tag. http://documentcloud.github.com/backbone/#View-elUPDATE
Starting v0.9.0, Backbone has view.setElement(element) to get this done.
In version 0.9.0, Backbone introduced
view.setElement(element)
to handle this operation.If you don't want to have the view wrap your HTML, you'll have to do a few things:
this.el
entirelydelegateEvents
on the newel
Since Backbone generates a
div
or other tag (based on yourtagName
setting for the view), you have to replace it entirely. That's easy to do. When you do that, though, you lose your declared events because Backbone uses jQuery'sdelegate
under the hood to wire them up. To re-enable your declared events, calldelegateEvents
and pass in your events declarations.The result is that your
view.el
will be the<option>
tag that you want, and nothing more.