-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Emit event when component has been removed #1434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/core/a-entity.js
Outdated
| pauseComponent(component, this.sceneEl); | ||
| component.remove(); | ||
| delete this.components[name]; | ||
| this.emit('componentremoved'); |
There was a problem hiding this comment.
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});There was a problem hiding this comment.
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.
|
Can you write a quick unit test for this in |
|
@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);
}); |
|
Don't need to create a Mesh, you should set a random component, use |
|
@ngokevin using |
|
In the test suite, there should already be a
|
|
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');
}); |
|
@ngokevin ohh i see |
|
yes just testing component detach |
|
@ngokevin i see. |
|
|
|
you might want something for detachedCallback, but this PR is ok too |
|
I'll check this PR out later and try to add a proper test. |
|
@ngokevin the test has been passed 🎉 |
| pauseComponent(component, this.sceneEl); | ||
| component.remove(); | ||
| delete this.components[name]; | ||
| this.emit('componentremoved', { name: name }); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 });
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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');
});|
Nice work. @mayognaise thanks! |
This PR is for this issue:
#1431
Additionally it's been attached
nameof component