Skip to content

Commit

Permalink
fix component.events singleton, events being overridden by components…
Browse files Browse the repository at this point in the history
… of the same type
  • Loading branch information
ngokevin committed Aug 7, 2019
1 parent 767e13f commit 64a4e93
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ var Component = module.exports.Component = function (el, attrValue, id) {
this.el.components[this.attrName] = this;
this.objectPool = objectPools[this.name];

eventsBind(this, this.events);
const events = this.events;
this.events = {};
eventsBind(this, events);

// Store component data from previous update call.
this.attrValue = undefined;
Expand Down Expand Up @@ -588,7 +590,7 @@ Component.prototype = {
function eventsBind (component, events) {
var eventName;
for (eventName in events) {
events[eventName] = events[eventName].bind(component);
component.events[eventName] = events[eventName].bind(component);
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/core/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,36 @@ suite('Component', function () {
});
});
});

test('does not collide with other component instances', function (done) {
registerComponent('test2', {
events: {
foo: function (evt) {
assert.equal(this.el.id, 'foo');
},

bar: function (evt) {
assert.equal(this.el.id, 'bar');
}
}
});

el.id = 'foo';
el.setAttribute('test2', '');

const el2 = document.createElement('a-entity');
el2.id = 'bar';
el2.setAttribute('test2', '');
el.appendChild(el2);

setTimeout(() => {
el.emit('foo', null, false);
el2.emit('bar', null, false);
setTimeout(() => {
done();
});
});
});
});
});

Expand Down

0 comments on commit 64a4e93

Please sign in to comment.