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-el
var PostView = Backbone.View.extend({ tagName: 'option' });
UPDATE
Starting v0.9.0, Backbone has view.setElement(element) to get this done.
var PostView = Backbone.View.extend({ initialize: function() { var template = _.template('<option value=""><%= title %></option>'); var html = template({title: 'post'}); this.setElement(html); } });
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 new el
render: function(){ var html = "some foo"; this.el = html; this.delegateEvents(this.events); }
Since Backbone generates a div
or other tag (based on your tagName
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's delegate
under the hood to wire them up. To re-enable your declared events, call delegateEvents
and pass in your events declarations.
The result is that your view.el
will be the <option>
tag that you want, and nothing more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With