I'm trying to learn typescript. In this attempt, I am trying to get an array of numbers, from a webservice I have created, into a javascript array.
I have the following Typescript class:
class GameMap2 {
Grid: Uint8Array;
width: number;
height: number;
constructor(height: number, width: number) {
this.height = height;
this.width = width;
this.Grid = new Uint8Array(height * width);
}
generateMap() {
alert("calling generate");
$.ajax({
url: "/api/Maps/" + this.width + "/" + this.height,
async: false,
dataType: 'json',
success: function(data) {
alert("Ajax success");
for (var idx = 0; idx < data.length; idx++) {
this.Grid[idx] = data[idx];
}
}
});
}
}
From the webservice, I will get something like: [1,1,1,1,1,1,1,1,1,0,0,0,1,0,0,1,1,1,1,0,1,0,1,1,1,0,0,0,1,0,0,1,1,0,1,1,1,0,1,1,1,0,0,0,0,0,0,1,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1]
Now, when I try to loop through this result, and put it into the Grid array, I get the error:
TypeError: Cannot set property '0' of undefined
If I change it to This.Grid.Push(data[idx]);
I get undefined errors.
It seems to me, that my Grid array is not actually within reach of my ajax callback. Can that really be true, or am I doing something wrong here?
Is there another way I can get my array into the javascript array?