Note that there are some explanatory texts on larger screens.

plurals
  1. POView Events not firing on created elements?
    text
    copied!<p>Trying to create a todo example app to mess around with backbone. I cannot figure out why the click event for the checkbox of a task is not firing. Here is my code for the TaskCollection, TaskView, and TaskListView:</p> <pre><code>$(document).ready(function() { Task = Backbone.Model.extend({}); TaskCollection = Backbone.Collection.extend({ model: 'Task' }); TaskView = Backbone.View.extend({ tagName: "li", className: "task", template: $("#task-template").html(), initialize: function(options) { if(options.model) { this.model = options.model } this.model.bind('change',this.render,this); this.render(); }, events: { "click .task-complete" : "toggleComplete" }, render: function(){ model_data = this.model.toJSON(); return $(_.template(this.template, model_data)); }, toggleComplete: function() { //not calling this function console.log("toggling task completeness"); } }); TaskListView = Backbone.View.extend({ el: $("#task-list"), task_views: [], initialize: function(options) { task_collection.bind('add',this.addTask,this); }, addTask: function(task){ task_li = new TaskView({'model' : task}); this.el.append(task_li.render()); this.task_views.push(task_li); }, }); }); </code></pre> <p>Template for the task:</p> <pre><code>&lt;script type='text/template' id='task-template'&gt; &lt;li class="task"&gt; &lt;input type='checkbox' title='mark complete' class='task-check' /&gt; &lt;span class='task-name'&gt;&lt;%= name %&gt;&lt;/span&gt; &lt;/li&gt; &lt;/script&gt; </code></pre> <p>I can't seem to figure out why the <code>toggleComplete</code> event will not fire for the tasks. how can I fix this?</p>
 

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