Skip to content

Commit

Permalink
fix repeatedly component init issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Algorush committed Jun 25, 2023
1 parent cdbd910 commit 0dcd8ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
40 changes: 21 additions & 19 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,41 +87,43 @@
schema: {
defaultURL: { type: 'string' }
},
init: async function () {
init: function () {
// get hash from window
const streetURL = window.location.hash.substring(1);
if (!streetURL) {
return;
}
if (streetURL.includes('streetmix')) {
if (streetURL.includes('//streetmix.net')) {
console.log('[set-loader-from-hash]','Using URL from hash', streetURL)
this.el.setAttribute('streetmix-loader', 'streetmixStreetURL', streetURL);
} else {
// try to load JSON file from remote resource
const jsonData = await this.fetchJSON(streetURL);
createElementsFromJSON(jsonData);
this.fetchJSON(streetURL);
}
// else {
// console.log('[set-loader-from-hash]','Using default URL', this.data.defaultURL)
// this.el.setAttribute('streetmix-loader', 'streetmixStreetURL', this.data.defaultURL);
// }
},
fetchJSON: async function (request) {
try {
const response = await fetch(request, {
mode: "no-cors",
credentials: "omit"
});
const contentType = response.headers.get("content-type");
if (!contentType || !contentType.includes("application/json")) {
console.log("Oops, we haven't got JSON!");
fetchJSON: function (requestURL) {
const request = new XMLHttpRequest();
request.open('GET', requestURL, true);
request.onload = function () {
if (this.status >= 200 && this.status < 400) {
// Connection success
// remove 'set-loader-from-hash' component from json data
const jsonData = JSON.parse(this.response,
(key, value) => (key === 'set-loader-from-hash') ? undefined : value );

console.log("loading 3D-Street from JSON file");
createElementsFromJSON(jsonData);
}
const jsonData = await response.json();
console.log("Loading scene from JSON file");
return jsonData;
} catch (error) {
console.error("Error:", error);
}
};
request.onerror = function () {
// There was a connection error of some sort
console.log('Loading Error: There was a connection error during JSON loading');
};
request.send();
}
});

Expand Down
6 changes: 6 additions & 0 deletions src/json-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ function getModifiedProperty (entity, componentName) {

function createEntities (entitiesData, parentEl) {
for (const entityData of entitiesData) {
if (entityData.id === 'street-container' &&
entityData.children &&
entityData.children[0].id === 'default-street' &&
entityData.children[0].components.hasOwnProperty('set-loader-from-hash')) {
delete entityData.children[0].components['set-loader-from-hash'];
}
createEntityFromObj(entityData, parentEl);
}
}
Expand Down

0 comments on commit 0dcd8ac

Please sign in to comment.