Skip to content

Commit a631ac7

Browse files
committed
fix component update getting overriden by mixin due to not passing in the whole attrValue into buildData (aframevr#3302)
1 parent ad24134 commit a631ac7

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

‎src/core/component.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,15 @@ Component.prototype = {
252252
// Cache previously passed attribute to decide if we skip type checking.
253253
this.previousAttrValue = attrValue;
254254

255+
// Cache current attrValue for future updates. Updates `this.attrValue`.
255256
attrValue = this.parseAttrValueForCache(attrValue);
256-
if (this.updateSchema) { this.updateSchema(this.buildData(attrValue, false, true)); }
257-
this.data = this.buildData(attrValue, clobber, false, skipTypeChecking);
258-
259-
// Cache current attrValue for future updates.
260257
this.updateCachedAttrValue(attrValue, clobber);
261258

259+
if (this.updateSchema) {
260+
this.updateSchema(this.buildData(this.attrValue, false, true));
261+
}
262+
this.data = this.buildData(this.attrValue, clobber, false, skipTypeChecking);
263+
262264
if (!this.initialized) {
263265
// Component is being already initialized.
264266
if (el.initializingComponents[this.name]) { return; }

‎tests/core/component.test.js‎

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,34 @@ suite('Component', function () {
222222
assert.equal(data[0], 'a');
223223
assert.notEqual(data, array);
224224
});
225+
226+
test('does not override set data with mixin', function (done) {
227+
var el;
228+
var mixinEl;
229+
230+
el = helpers.entityFactory();
231+
232+
registerComponent('dummy', {
233+
schema: {
234+
color: {default: 'blue'},
235+
size: {default: 5}
236+
}
237+
});
238+
239+
mixinEl = document.createElement('a-mixin');
240+
mixinEl.setAttribute('id', 'mixindummy');
241+
mixinEl.setAttribute('dummy', 'size: 1');
242+
243+
el.addEventListener('loaded', () => {
244+
el.sceneEl.appendChild(mixinEl);
245+
el.setAttribute('mixin', 'mixindummy');
246+
el.setAttribute('dummy', 'size', 20);
247+
el.setAttribute('dummy', 'color', 'red');
248+
assert.equal(el.getAttribute('dummy').color, 'red');
249+
assert.equal(el.getAttribute('dummy').size, 20);
250+
done();
251+
});
252+
});
225253
});
226254

227255
suite('updateProperties', function () {
@@ -406,7 +434,7 @@ suite('Component', function () {
406434
assert.equal(el.components.dummy.data, el.components.dummy.schema.default);
407435
});
408436

409-
test('a plain object schema default is cloned into data', function () {
437+
test('clones plain object schema default into data', function () {
410438
registerComponent('dummy', {
411439
schema: {type: 'vec3', default: {x: 1, y: 1, z: 1}}
412440
});
@@ -418,21 +446,25 @@ suite('Component', function () {
418446
assert.deepEqual(el.components.dummy.data, {x: 1, y: 1, z: 1});
419447
});
420448

421-
test('do not clone properties from attrValue into data that are not plain objects', function () {
449+
test('does not clone properties from attrValue into data that are not plain objects', function () {
450+
var attrValue;
451+
var data;
452+
var el;
453+
422454
registerComponent('dummy', {
423455
schema: {
424456
color: {default: 'blue'},
425457
direction: {type: 'vec3'},
426458
el: {type: 'selector', default: 'body'}
427459
}
428460
});
429-
var el = document.createElement('a-entity');
461+
el = document.createElement('a-entity');
430462
el.hasLoaded = true;
431463
el.setAttribute('dummy', '');
432464
assert.notOk(el.components.dummy.attrValue.el);
433-
// The direction property will be preserved
434-
// across updateProperties calls but cloned
435-
// into a different object
465+
466+
// Direction property preserved across updateProperties calls but cloned into a different
467+
// object.
436468
el.components.dummy.updateProperties({
437469
color: 'green',
438470
direction: {x: 1, y: 1, z: 1},
@@ -442,16 +474,16 @@ suite('Component', function () {
442474
color: 'red',
443475
el: document.head
444476
});
445-
var data = el.getAttribute('dummy');
446-
var attrValue = el.components.dummy.attrValue;
477+
data = el.getAttribute('dummy');
478+
attrValue = el.components.dummy.attrValue;
447479
assert.notEqual(data, attrValue);
448480
assert.equal(data.color, attrValue.color);
449481
// HTMLElement not cloned in attrValue, reference is shared instead.
450482
assert.equal(data.el, attrValue.el);
451483
assert.equal(data.el.constructor, HTMLHeadElement);
452-
assert.notEqual(data.direction, attrValue.direction);
453484
assert.deepEqual(data.direction, {x: 1, y: 1, z: 1});
454485
assert.deepEqual(attrValue.direction, {x: 1, y: 1, z: 1});
486+
455487
el.components.dummy.updateProperties({
456488
color: 'red',
457489
direction: {x: 1, y: 1, z: 1}

0 commit comments

Comments
 (0)