I am using ES6 and have a problem, showCarOnMap() that a part of instance is not accessible inside $(document).delegate() or $(document).click()
Actually I have a table and found the row number after click through $(document).delegate() by "$(this).index()" and I want to call showCarOnMap() function after founding the clicked row number. but this time 'this' is not having showCarOnMap();
Please see the comments for better understanding.
import {Page} from './framework/page.js';
import {DataTable} from './ui/data-table.js';
import {application} from './app.js';
import {GoogleMap} from './ui/google-map.js';
import {Button} from './ui/button.js';
export class CarsPage extends Page{
constructor(){
super('Cars');
}
createElement(){
super.createElement();
//passing headers and data to genrate the table of car
let headers = 'License Make Model Miles Track'.split(' ');
let t = new DataTable(headers, application.dataService.cars);
t.appendToElement(this.element);
/* Poblem Start here*/
$(document).delegate("tr", "click", function(e) {
let index = $(this).index(); // getting right index
this.showCarOnMap(index); // BUT: not getting showCarOnMap()
//Uncaught TypeError: this.showCarOnMap is not a function
});
}
showCarOnMap(index){
// car on map
let mapArray = [];
mapArray.push(application.dataService.cars[index]);
let centerOfMap = {lat: 40.783661, lng: -73.965883};
let carLabelObject = {titleOfCar:'Car', titleOfDrone:'Drone'};
let carMap = new GoogleMap(centerOfMap, mapArray, carLabelObject);
carMap.appendToElement(this.element);
}
getElementString(){
return '<div style="margin:20px;"><h3>Cars</h3></div>';
}
}
I tried one another way- But not working
$(document).delegate("tr", "click",(e)=>{
let index = $(this).index(); // wrong: index always -1
this.showCarOnMap(index); // founded in this });