Danny
Danny

Reputation: 9634

Add data to local array in javascript

I've been trying to add data into my array for sometime now and it doesn't work. I have the following code:

function OBJMesh(file)
{
    this.modelVertex = [];
    this.modelColor = [];
    var that = this;
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function ()
    {

        if(rawFile.readyState == 4)
        {
            if(rawFile.status === 200 || rawFile.status === 0)
            {
                var allText = rawFile.responseText;
                var lines = allText.split("\n");


                for(var i = 0; i < lines.length; i ++)
                {
                    var lineData = lines[i];
                    var lineString = lineData.split(" ");

                    if(lineString[0] === "v")
                    {

                        var x = parseFloat(lineString[1]);
                        var y = parseFloat(lineString[2]);
                        var z = parseFloat(lineString[3]);

                        /*
                        this.modelVertex.push(x);
                        this.modelVertex.push(y);
                        this.modelVertex.push(z);

                        this.modelColor.push(0.0);
                        this.modelColor.push(0.0);
                        this.modelColor.push(0.0);
                        this.modelColor.push(1.0);
                        */

                        that.modelVertex.push(10.0);

                        //document.getElementById("textSection").innerHTML = "testing";
                    }

                }
            }
        }
    }

    rawFile.send();

}

OBJMesh.prototype.getModelVertex = function ()
{
    return this.modelVertex;
};

OBJMesh.prototype.getModelColor = function ()
{
    return this.modelColor;
};

If I comment out the this.modelVertex.push(10.0); it gets pass the error and prints out "testing". But if I uncomment it, it gets stuck there and won't print anything out. Why is it doing this? how can I solve it so it actually pushes the given data to the this.modelVertex array?

Many Thanks

Edit: I have edited my code after dystroy told me what to do and it do work when I try to print the values in the OBJMesh constructor (shown above), but when I try to do this by creating the object in my main function (shown below) it doesn't print anything.

var cubeModel;

function main()
{
    cubeModel = new OBJMesh("file:///Users/Danny/Desktop/3DHTMLGame%202/cube.obj");

    document.getElementById("textSection").innerHTML = cubeModel.getModelVertex();
}

Upvotes: 1

Views: 321

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382168

this isn't your new instance of OBJMesh in the callback but the XMLHttpRequest.

Start by referencing the desired object just before defining the callback :

var that = this;
rawFile.onreadystatechange = function ()

then use it :

that.modelVertex.push(10.0);

Answer to the question in your edit :

Your constructor contains an asynchronous request. Which means your array isn't available immediately but later.

A solution would be to pass a callback to the constructor :

function OBJMesh(file, doAfterInit) {
    this.modelVertex = [];
    this.modelColor = [];
    var that = this;
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function () {
        if(rawFile.readyState == 4)
        {
            if(rawFile.status === 200 || rawFile.status === 0)
            {
                var allText = rawFile.responseText;
                var lines = allText.split("\n");
               for(var i = 0; i < lines.length; i ++)
                {
                    var lineData = lines[i];
                    var lineString = lineData.split(" ");
                    if(lineString[0] === "v"){
                        that.modelVertex.push(10.0);
                        if (doAfterInit) doAfterInit();
                    }
                }
            }
        }
    }
    rawFile.send();
}

...


cubeModel = new OBJMesh("file:///Users/Danny/Desktop/3DHTMLGame%202/cube.obj", function() {
    document.getElementById("textSection").innerHTML = cubeModel.getModelVertex();
});

But having a class here doesn't look like a smart idea.

Upvotes: 5

Related Questions