Skip to content

Conversation

@mayognaise
Copy link
Contributor

@mayognaise mayognaise commented May 3, 2016

This PR is for this issue:
#1431

Additionally it's been attached name of component

pauseComponent(component, this.sceneEl);
component.remove();
delete this.components[name];
this.emit('componentremoved');
Copy link
Member

@donmccurdy donmccurdy May 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be helpful to attach the name of the component, as well –

this.emit('componentremoved', {name: name});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@donmccurdy great idea let me add it.

@ngokevin
Copy link
Member

ngokevin commented May 4, 2016

Can you write a quick unit test for this in a-entity.test.js? Thanks!

@mayognaise
Copy link
Contributor Author

mayognaise commented May 4, 2016

@ngokevin hmm actually the test hasn't been passed.. the event is not called :/ do you think it's because it's already removed?

  test('emits `componentremoved` event when element itself has been removed', function (done) {
    var el = document.createElement('a-entity');
    var parentEl = this.el;
    el.object3D = new THREE.Mesh();
    el.addEventListener('componentremoved', function () {
      done();
    });
    el.addEventListener('loaded', function () {
      el.parentNode.removeChild(el);
    });
    parentEl.appendChild(el);
  });
@ngokevin
Copy link
Member

ngokevin commented May 4, 2016

Don't need to create a Mesh, you should set a random component, use removeAttribute on it, and check the event detail has the component name. You can use the existing entity created in the setup this.el

@mayognaise
Copy link
Contributor Author

mayognaise commented May 4, 2016

@ngokevin using entityFactory();?
and why using removeAttribute ?
im afraid to ask if you give me an example... 😨 sorry im not good at this..

@ngokevin
Copy link
Member

ngokevin commented May 4, 2016

In the test suite, there should already be a setup that calls entityFactory for you.

removeAttribute detaches components, which is what you're testing here. removeChild would detach all components I guess, but it's simpler to test with removeAttribute.

@ngokevin
Copy link
Member

ngokevin commented May 4, 2016

ex:

  test('emits `componentremoved` event when element itself has been removed', function (done) {
    var el = this.el;
    el.setAttribute('geometry');
    el.addEventListener('componentremoved', function (evt) {
      assert.equal(evt.detail.name, 'geometry');
      done();
    });
    el.removeAttribute('geometry');
  });
@mayognaise
Copy link
Contributor Author

mayognaise commented May 4, 2016

@ngokevin ohh i see
so this doesn’t mean when the a-entity has been removed from dom, correct?

@ngokevin
Copy link
Member

ngokevin commented May 4, 2016

yes just testing component detach

@mayognaise
Copy link
Contributor Author

mayognaise commented May 4, 2016

@ngokevin i see. componentremoved hasn't been fired.. 😭
I may be doing a wrong PR, but what I expected is to know when a-entity has removed from shader side, like component has remove function.

@ngokevin
Copy link
Member

ngokevin commented May 4, 2016

el.setAttribute('geometry', ''); fixed this line

@ngokevin
Copy link
Member

ngokevin commented May 4, 2016

you might want something for detachedCallback, but this PR is ok too

@ngokevin
Copy link
Member

ngokevin commented May 4, 2016

I'll check this PR out later and try to add a proper test.

@mayognaise
Copy link
Contributor Author

@ngokevin the test has been passed 🎉

pauseComponent(component, this.sceneEl);
component.remove();
delete this.components[name];
this.emit('componentremoved', { name: name });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we pass the entity as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we should pass it. it would be done this way:
this.emit('componentremoved', { el: this, name: name });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmarcos sure let me add el

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it already be available as event.target? that seems like enough if so, otherwise maybe helpful to add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah true, let me check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has passed. so maybe dont we need to add it?

  test('emits `componentremoved` event when element itself has been removed', function (done) {
    var el = this.el;
    el.setAttribute('geometry', 'primitive:plane');
    el.addEventListener('componentremoved', function (event) {
      assert.equal(event.target, el);
      assert.equal(event.detail.name, 'geometry');
      done();
    });
    el.removeAttribute('geometry');
  });
@dmarcos
Copy link
Member

dmarcos commented May 7, 2016

Nice work. @mayognaise thanks!

@dmarcos dmarcos merged commit dd8b478 into aframevr:master May 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants