javascript - Scoping of 'this' in TypeScript -
i have simple class, run pain definition of ‘this’ in typescript:
typescript
/// <reference path='jquery.d.ts' /> /// <reference path='bootstrap.d.ts' /> module problem { export class index { detailsurl: string; constructor() { $('.problem-detail-button').click((e) => { e.preventdefault(); var $row = $(this).closest('tr'); //this must of callback var problemid: number = $row.data('problem-id'); $.ajax({ url: this.detailsurl, //this must instance of class data: { id: problemid }, type: 'post', success: (result) => { $('#details-modal-placeholder').html(result); $('#details-modal-placeholder modal').modal('show'); }, }) }); } } } javascript
var problem; (function (problem) { var index = (function () { function index() { var _this = this; $('.problem-detail-button').click(function (e) { e.preventdefault(); var $row = $(_this).closest('tr'); var problemid = $row.data('problem-id'); $.ajax({ url: _this.detailsurl, data: { id: problemid }, type: 'post', success: function (result) { $('#details-modal-placeholder').html(result); $('#details-modal-placeholder modal').modal('show'); } }); }); } return index; })(); problem.index = index; })(problem || (problem = {})); now problem line
var $row = $(this).closest('tr'); //this must of callback and line
this.detailsurl, //this must instance of class conflict in meaning of 'this'
how handle mixture of 'this'?
module problem { export class index { detailsurl: string; constructor() { var = this; $('.problem-detail-button').click(function (e) { e.preventdefault(); var $row = $(this).closest('tr'); //this must of callback var problemid: number = $row.data('problem-id'); $.ajax({ url: that.detailsurl, //this must instance of class data: { id: problemid }, type: 'post', success: (result) => { $('#details-modal-placeholder').html(result); $('#details-modal-placeholder modal').modal('show'); }, }) }); } } } explicitly declare that = this have reference that.detailsurl, don't use fat arrow click handler, correct this scope callback.
Comments
Post a Comment