<div class="view">
<input id="todo_complete" type="checkbox" <%= completed ? 'checked="checked"' : '' %> />
<label><%= title %></label>
<button class="destroy"></button>
</div>
<input class="edit" value="<%= title %>" />
查看:
var TodoView = Backbone.View.extend({
tagName: 'li',
todoTpl: _.template($('#item-template').html()),
events: {
'dblclick label': 'edit',
'keypress .edit': 'updateOnEnter',
'blur .edit': 'close'
},
initialize: function() {
console.log('Todo View initialized!');
this.$el = $('#todo');
console.log(this.$el);
this.render();
},
render: function() {
console.log('Todo View render started...');
console.log(this.model.attributes);
this.$el.html(this.todoTpl(this.model.attributes));
},
edit: function() {console.log('edit called!');},
close: function() {console.log('close called!');},
updateOnEnter: function(e) {
console.log(e);
}
});
並無事件在頁面加載時,我能看到渲染模板。但是,如果我雙擊label
,按下input
或模糊其他輸入,則什麼都不會發生。我期待在控制檯中看到日誌。我究竟做錯了什麼?
嗯......它似乎很奇怪,ID必須將視圖添加到DOM **沒有**使用主幹。我必須使用jQuery來做到這一點嗎? – dopatraman
通常情況下,代碼將位於路由器或您的應用程序初始化代碼中。 –