Nikita Fedorov
Nikita Fedorov

Reputation: 870

aframe-state component: remove function not called on removing entities produced by bind-for

I have such template :

<a-entity bind-for="for: item; in: creatures; key: id; updateInPlace: true">
  <template>
    <a-entity 
        bind-item__uw-id="item.id"
        bind-item__position="item.position"
        bind-item__rotation="item.rotation"
        bind-item__scale="item.scale"
        bind-item__uw-name="item.name"
        bind-item__gltf-model="item.url"
        animation-mixer></a-entity>
  </template>
</a-entity

After some event I want to remove all the entities from my scene. I'm trying to do it the following way:

export const cleanScene = () => {
    const camera = document.querySelector('a-camera')
    if (camera) {
        camera.parentNode.removeChild(camera);
        camera.destroy();
    }

    removeAframEntities('a-entity[bind-item__uw-id]');
    removeAframEntities('a-entity[bind-for]');
    removeAframEntities('a-entity');
    // document.querySelector('a-scene').systems.state.subscriptions = []; // - workaround
}

function removeAframEntities(selector) {
    document.querySelectorAll(selector).forEach(e => {
        e.parentNode.removeChild(e);
        if (e.destroy) {
            e.destroy();
        }
    });
}

Then when I dispatch an event to the state, I'm getting the following error:

Uncaught TypeError: Cannot read property 'emit' of null
    at NewComponent.renderItemsInPlace (aframe-state-component.js:954)
    at NewComponent.<anonymous> (aframe-state-component.js:906)
    at NewComponent.onStateUpdate (aframe-state-component.js:1039)
    at NewSystem.<anonymous> (aframe-state-component.js:367)

This is because there is no entity to update, but there are still subscribers in the state. Apparently the remove() function wasn't called for the entities produced by the template loop (all the other subscribers are gone). Previously I was removing the entities by only calling removeAframEntities('a-entity');, I tried to explicitly remove the produced entities and the one with bind-for attribute, but it didn't solve my issue. What I'm doing wrong?

Update

Just checked the components code and it turned out that bind-for and bind-item components don't even have a remove() function. How they're supposed to be removed?

Upvotes: 2

Views: 368

Answers (1)

Nikita Fedorov
Nikita Fedorov

Reputation: 870

The bug was fixed here: https://github.com/supermedium/superframe/pull/279, I haven't checked if it was released though.

Upvotes: 1

Related Questions