Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone view with template not firing events
    primarykey
    data
    text
    <p>I'm having some problems with my view handling it's events. They simply don't fire. The view has it's html from a template that looks like this:</p> <pre><code>&lt;script type="text/template" id="intro-slide-template"&gt; &lt;div class="slide intro" id="intro-slide"&gt; &lt;a href="#" class="startpresentation" id="start-btn"&gt; &lt;%= startpresentation %&gt; &lt;/a&gt; &lt;/div&gt; &lt;/script&gt; </code></pre> <p>So far so good. The view code itself look's like this:</p> <pre><code> /* ********* 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"); } }); </code></pre> <p>This is how I render the slide to the main app's view:</p> <pre><code>/* ********* 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; } }); </code></pre> <p>The view is getting rendered to the screen perfectly, and it works just like it should. But the click event is not firing.</p> <p>When I inspect the slide view object, I can see that it do haves el object with the right HTML in it, including the button that has to fire a click event.</p> <p>And when I log the view's events, I see that they are set:</p> <pre><code>Object click #start-btn: "clickHandler" __proto__: Object </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload