wowpatrick
wowpatrick

Reputation: 5180

Backbone.js - Render a collection as HTML option tags

A have a collection of items I'd like to render into an existing <select> elment in the template. The problem I have is that the view always wraps the <option> list in a DIV tag. How can I only render the <option> list without any wrapping element?

Template:

<script type="text/template" id="template-select">
    <% _(elements).each(function(element) { %>
            <option value="<%= element.id %>"><%= element.name %></option>
    <% }); %>
</script>

View:

myView = Backbone.View.extend({
    template: template('template-select'),
    render: function() {
        this.$el.html(this.template({
                elements: this.collection.toJSON()
            }));
        return this;
    }
});

Upvotes: 0

Views: 349

Answers (1)

Paul Hoenecke
Paul Hoenecke

Reputation: 5060

I assume somewhere else you are creating an instance of that view add rendering/adding it to the <select> element.

You can try something like this instead:

var view = new myView({el: '#id-of-existing-select'});
view.render();

This would make the view use the existing <select> instead of generating the <div>.

Upvotes: 2

Related Questions