Hi everyone.
I’m currently sitting with this problem in my recursive method i’m creating that will go through an array structure to create a multidimensional object which will then be used in a Vue template once processed. What this array contains is a component object which Vue uses on the template and also some other information relevant to what i’m making…etc.
Now the things that i’m having a problem with i think is called a variable callback scope problem, I will explain what is the setup and then the problem.
In my process, what happens is an ajax call collects the array and data which will be going inside the array from the backend, In the success callback of the ajax call i have a method which is outside of the ajax call, which is being called to perform the recursive processes.
The problem i’m having is the last value from my data is being set on two of the vue components, this i think is due to the async method which is ajax, i read that because it’s async the for loops i’m using set the var’s inside the loop params in the global space and then the code get’s executed once the async method is done, or something along those lines.
Example of my code:
Array format
[
[
[
{
'component': object
}
]
]
[
[
{
'component': object
}
]
]
]
That may look a bit weird with all the nested arrays but there is a reason for this, the system i’m making is for creating bootStrap layouts and the array is the layout itself.
The method i have that’s doing the recursive processing:
function doAllTheTHings($data)
{
$returnArray = [];
for(var $index in $data)
{
if(some conditions)
{
$returnArray[$index] = componentObjectData;
}
else if(x is not there do the recursive part)
{
$retyrbArray[$index] = doAllTheThings($data[$index]);
}
}
return $returnArray;
}
Please note thins is a rough draft to what it’s really life, and there is also some things in place to make sure the recursive process does not go on forever.
Do any of you know how i can solve this problem? i need to not have the for loop duplicate / use the last data in the array for both components’s data