Trunk view with template doesn't fire events
I am having trouble viewing events. They just don't shoot. The view has html from a template that looks like this:
<script type="text/template" id="intro-slide-template">
<div class="slide intro" id="intro-slide">
<a href="#" class="startpresentation" id="start-btn">
<%= startpresentation %>
</a>
</div>
</script>
So far so good. The view code itself looks like this:
/* ********* Slide View ********* */
window.IntroSlideView = Backbone.View.extend({
initialize: function(){
this.template = _.template($("#intro-slide-template").html());
},
render: function(){
$(this.el).html(this.template(this.model.toJSON()));
},
events: {
"click #start-btn": "clickHandler"
},
clickHandler: function() {
console.log("IntroSlideView :: clickHandler");
}
});
This is how I present the slide in the main application window:
/* ********* App View ********* */
window.AppView = Backbone.View.extend({
el: $("#content"),
initialize: function(){
_.bindAll(this, 'render');
//render
this.render();
},
render: function(){
this.el.innerHTML = "";
var slide = this.model.get("currentSlide");
slide.render();
this.el.innerHTML = slide.el.innerHTML;
}
});
The view gets rendered on the screen fine and it works as it should. But the click event doesn't fire.
When I inspect the slide view object, I see that it contains an el object with the desired HTML, including the button that should fire the click event.
And when I log the view events, I see that they are set:
Object
click #start-btn: "clickHandler"
__proto__: Object
+3
source to share
1 answer
Try this pattern
window.AppView = Backbone.View.extend({
render: function() {
this.slide = new window.IntroSlideView({ el: this.$(".slide") });
// ...
}
});
0
source to share