jqueryとModel View ViewModelを使い始めましたが、これをEvent Handler Attachment:on()で使用する際に問題が発生しました。メソッドへのアクセス中に問題が発生しました
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
//...
私は最初のクラスに依存し、DOMの操作とゲームのグラフィック部分を管理する別のクラス、TicTacToeControllerを持っている:
私の最初のクラスは、メモリ内に三目並べゲームを操作TicTacToeModelです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()。しかし、これは.on()関数の外で動作しているようですので、問題の原因となるのは、this
の悪い利用を想定しています。
あなたの答えをありがとう、それはうまく動作しますが、私はそのような関数にいくつかのjqueryを追加する: 'TicTacToeController.prototype.addListeners = function(){ this._model.on( 'change'、this.render、this ); this._element.on( 'click'、 "td"、function(){ $(this).addClass( "case"); $(this).html( "X"); this._model。再生(0,0); //動作しません } .bind(this)); }; 'jqueryは考慮されません。修正する考えはありますか? – Lodec
「jqueryは考慮されていませんが、あなたは '$(this.)の代わりに' $(this._element)... 'を試しましたか? – Jack
'$(this._element)'は私のグリッド全体を変更するので動作しませんが、 'this._element.on( 'click'、" td "、function(event){なぜなら、これは.on()関数の "td"要素を表していますか? – Lodec