Here's a template I'm using
<script type="text/template" id="template-message">
<p class="message"><%= message %></p>
</script>
Here's my Backbone.View
var MessageView = Backbone.View.extend({
template: _.template(Backbone.$("#template-message").html()),
render: function() {
this.$el.html(this.template(this.model.attributes));
return this;
},
});
How I'm using it
var msg = new Message({message: "hello"}),
view = new MessageView({model: msg});
$("body").append(view.render().el);
Output
<body>
<div>
<p class="message">
hello
</p>
</div>
</body>
I understand why it's happening (the default tagName for a view is div
), but I don't want the wrapper div
on there. I would like to see this as the output
<body>
<p class="message">
hello
</p>
</body>
I'm aware that I could have this in my view
var MessageView = Backbone.View.exntend({
tagName: "p",
className: "message",
// ...
});
The end user will be define the templates, so I don't want those things to be specified in the Backbone.View source code.
How can I get the desired output and still bind the view to the html?