Skip to content

Commit c070896

Browse files
committed
Set this.oldData before calling update to enable recursive setAttribute within the updat method
1 parent 34258b9 commit c070896

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

‎src/core/component.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,11 @@ Component.prototype = {
266266
// We pass empty object to multiple property schemas and single property schemas that parse to objects like position, rotation, scale
267267
// undefined is passed to the rest of types.
268268
oldData = (!isSinglePropSchema || typeof parseProperty(undefined, this.schema) === 'object') ? {} : undefined;
269+
// Store current data as previous data for future updates.
270+
this.oldData = extendProperties({}, this.data, isSinglePropSchema);
269271
this.update(oldData);
270272
// Play the component if the entity is playing.
271273
if (el.isPlaying) { this.play(); }
272-
// Store current data as previous data for future updates.
273-
this.oldData = extendProperties({}, this.data, isSinglePropSchema);
274274
el.emit('componentinitialized', {
275275
id: this.id,
276276
name: this.name,

‎tests/core/component.test.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,26 @@ suite('Component', function () {
674674
assert.deepEqual(updateStub.getCalls()[1].args[0], {x: 1, y: 1, z: 1});
675675
assert.deepEqual(el.components.dummy.data, {x: 2, y: 2, z: 2});
676676
});
677+
678+
test('oldData and data is properly passed on recursive calls to setAttribute', function () {
679+
var el = this.el;
680+
registerComponent('dummy', {
681+
schema: {
682+
color: {default: 'red'},
683+
size: {default: 0}
684+
},
685+
update: function (oldData) {
686+
if (this.data.color === 'red') {
687+
this.el.setAttribute('dummy', 'color', 'blue');
688+
}
689+
if (oldData.color === 'red') {
690+
this.el.setAttribute('dummy', 'color', 'green');
691+
}
692+
}
693+
});
694+
el.setAttribute('dummy', 'color: red');
695+
assert.equal(el.getAttribute('dummy').color, 'green');
696+
});
677697
});
678698

679699
suite('flushToDOM', function () {

0 commit comments

Comments
 (0)