バックボーンで混乱させるtodoサンプルアプリケーションを作成しようとしています。私は、タスクのチェックボックスのクリックイベントがなぜ発火していないのか理解できません。ここでTaskCollection、TaskView、およびTaskListViewのための私のコードは次のとおりです。タスクのViewイベントは作成された要素で発生しませんか?
$(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);
},
});
});
テンプレート:
<script type='text/template' id='task-template'>
<li class="task">
<input type='checkbox' title='mark complete' class='task-check' />
<span class='task-name'><%= name %></span>
</li>
</script>
私はtoggleComplete
イベントは、タスクのために発生しません理由を把握することができないよう。どうすればこの問題を解決できますか?
私の答えはあなたのために働くのか、それとも明確ではないのですか? – Wieczo