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.

playground.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -