Существует класс под названием JSONHeader, с которого мы загружаем каждый из JSON:
import jQuery from 'jquery-ajax';
import Atlas from "./Atlas";
export default class JSONHeader {
constructor(location) {
loadAtlasStructure(location);
function loadAtlasStructure(location) {
jQuery.ajax({
dataType: "json",
url: location,
async: true,
success: function (files) {
files.map((file) => {
jQuery.ajax({
dataType: "json",
url: location + file,
async: true,
success: function (data) {
console.info(data);
if (!window.IndexAtlas) {
window.IndexAtlas = new Atlas();
}
window.IndexAtlas.addSubAtlas(data);
console.info('JSONHeader::window.IndexAtlas', window.IndexAtlas);
}
});
})
}
})
}
}
}
И я хотел бы изменить компонент верхнего уровня React: App с isLoading: true на isLoading: false после загрузки всех json. Как мы могли добиться такого поведения? В настоящее время я заставляю ждать 5 секунд каждый раз, когда мы перезагружаем все JSON.
import React, {Component} from 'react';
import BrowserRouter from "react-router-dom/es/BrowserRouter";
import Switch from "react-router-dom/es/Switch";
import Route from "react-router-dom/es/Route";
import Redirect from "react-router-dom/es/Redirect";
import ScenePage from "../ScenePage/index";
import CoverPage from "../CoverPage/index";
import {INDEX, SCENE_PAGE} from "../../constantRoutes";
import JSONHeader from "../../newModel14Junio/JSONHeader";
export default class App extends Component {
constructor(props) {
super(props);
const header = new JSONHeader('/atlas/json/');
this.state = {
isAtlasLoading: true
};
setTimeout(() => {
this.setState({isAtlasLoading: false});
}, 5000);
}
render() {
if (this.state.isAtlasLoading) {
return (<div>Atlas loading</div>);
}
return (
<BrowserRouter>
<div>
<Switch>
<Route exact path = {INDEX} component = {CoverPage}/>
<Route path = {SCENE_PAGE} component = {() => <ScenePage IndexAtlas = {window.IndexAtlas}/>}/>
<Redirect from = "*" to = {INDEX}/>
</Switch>
</div>
</BrowserRouter>
);
}
}
Спасибо за помощь.



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Использовать для этого тайм-аут - не лучшая идея, потому что вы не знаете, сколько времени уйдет на завершение. Итак, вы можете добавить обратный вызов в свой JSONHeader, например:
import jQuery from 'jquery-ajax';
import Atlas from "./Atlas";
export default class JSONHeader {
constructor(location, callback) {
loadAtlasStructure(location);
function loadAtlasStructure(location, callback) {
jQuery.ajax({
dataType: "json",
url: location,
async: true,
success: function (files) {
files.map((file) => {
jQuery.ajax({
dataType: "json",
url: location + file,
async: true,
success: function (data) {
console.info(data);
if (!window.IndexAtlas) {
window.IndexAtlas = new Atlas();
}
window.IndexAtlas.addSubAtlas(data);
console.info('JSONHeader::window.IndexAtlas', window.IndexAtlas);
callback();
}
});
})
}
})
}
}
}
В вашем ReactJS вы просто создаете обратный вызов и передаете его при вызове JSONHeader:
import React, {Component} from 'react';
import BrowserRouter from "react-router-dom/es/BrowserRouter";
import Switch from "react-router-dom/es/Switch";
import Route from "react-router-dom/es/Route";
import Redirect from "react-router-dom/es/Redirect";
import ScenePage from "../ScenePage/index";
import CoverPage from "../CoverPage/index";
import {INDEX, SCENE_PAGE} from "../../constantRoutes";
import JSONHeader from "../../newModel14Junio/JSONHeader";
export default class App extends Component {
constructor(props) {
super(props);
const finishCallback = () => {
this.setState({isAtlasLoading: false});
};
const header = new JSONHeader('/atlas/json/', finishCallback);
this.state = {
isAtlasLoading: true
};
}
render() {
if (this.state.isAtlasLoading) {
return (<div>Atlas loading</div>);
}
return (
<BrowserRouter>
<div>
<Switch>
<Route exact path = {INDEX} component = {CoverPage}/>
<Route path = {SCENE_PAGE} component = {() => <ScenePage IndexAtlas = {window.IndexAtlas}/>}/>
<Redirect from = "*" to = {INDEX}/>
</Switch>
</div>
</BrowserRouter>
);
}
}
Я надеюсь, что помог тебе.
Вы можете передать обратный вызов JSONHeader, и там, когда он закончится, вы вызовете его