使用vanillaJS阅读基本todoMVC构建的一部分时遇到一些麻烦: Here is the link
所以在view.js中,我们看到了
View.prototype.bind = function (event, handler) {
var self = this;
[...]
} else if (event === 'itemToggle') {
$delegate(self.$todoList, '.toggle', 'click', function () {
handler({
id: self._itemId(this),
completed: this.checked
});
});
现在this
。检查,我不完全确定程序实际运行时将引用哪个范围this
。它只是被点击的DOM对象吗?因为它将是运行处理函数的那个??
在控制器下我们有
function Controller(model, view) {
var self = this;
self.model = model;
self.view = view;
[...]
self.view.bind('itemToggle', function (item) {
self.toggleComplete(item.id, item.completed);
});
}
Controller.prototype.toggleComplete = function (id, completed, silent) {
var self = this;
self.model.update(id, { completed: completed }, function () {
self.view.render('elementComplete', {
id: id,
completed: completed
});
});
if (!silent) {
self._filter();
}
};