【发布时间】:2017-04-08 00:09:46
【问题描述】:
我开始使用 jquery 和 Model View ViewModel,但在使用 Event Handler Attachment 时遇到了问题:on()。 我的第一堂课是 TicTacToeModel,它在内存中操作 TicTacToe 游戏:
var TicTacToeModel = function () {
Observable.call(this);
this.grid = new Array(3);
for (let i = 0; i < this.grid.length; ++i) {
this.grid[i] = new Array(3);
}
};
//Some prototype function
//...
我有另一个类,TicTacToeController,它依赖于第一个类,它通过对 DOM 的操作来管理游戏的图形部分:
var TicTacToeController = function(game, selector) {
this._element = $(selector);
this._model = game;
this._template = Handlebars.compile(view);
this.addListeners();
this.render();
};
(游戏声明:game = new TicTacToeModel();)
所以在我的第二堂课中,我有这个功能:
TicTacToeController.prototype.addListeners = function() {
this._model.on('change', this.render, this);
this._model.play(0,0);//works
this._element.on('click', "td", function() {
this._model.play(0,0);//doesn't work
});
};
当我在图形界面中单击单元格时,我想在单元格 (0,0) 中调用 play() 函数(函数 play 更新内存中的游戏),但我无法在 .在()。但这似乎在 .on() 函数之外起作用,所以我认为 this 的使用不当会导致问题。
【问题讨论】:
标签: javascript jquery object this