From 141f60aea7c4c6b2326c0fac2c21a84f7374055e Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Thu, 26 Oct 2017 11:03:35 +0200 Subject: [PATCH] Make generic _set aware of real change property (#4415) * first change * added a small test --- src/shapes/object.class.js | 7 ++++--- test/unit/object.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/shapes/object.class.js b/src/shapes/object.class.js index af0782cf8a2..cc850472f87 100644 --- a/src/shapes/object.class.js +++ b/src/shapes/object.class.js @@ -884,7 +884,8 @@ * @return {fabric.Object} thisArg */ _set: function(key, value) { - var shouldConstrainValue = (key === 'scaleX' || key === 'scaleY'); + var shouldConstrainValue = (key === 'scaleX' || key === 'scaleY'), + isChanged = this[key] !== value; if (shouldConstrainValue) { value = this._constrainScale(value); @@ -906,14 +907,14 @@ this[key] = value; - if (this.cacheProperties.indexOf(key) > -1) { + if (isChanged && this.cacheProperties.indexOf(key) > -1) { if (this.group) { this.group.set('dirty', true); } this.dirty = true; } - if (this.group && this.stateProperties.indexOf(key) > -1 && this.group.isOnACache()) { + if (isChanged && this.group && this.stateProperties.indexOf(key) > -1 && this.group.isOnACache()) { this.group.set('dirty', true); } diff --git a/test/unit/object.js b/test/unit/object.js index 22378a776de..5a5866ef29f 100644 --- a/test/unit/object.js +++ b/test/unit/object.js @@ -1225,4 +1225,22 @@ object.shadow.offsetX = 1; assert.equal(object.willDrawShadow(), true, 'object will drawShadow'); }); + + QUnit.test('_set change a property', function(assert) { + var object = new fabric.Object({ fill: 'blue' }); + object._set('fill', 'red'); + assert.equal(object.fill, 'red', 'property changed'); + }); + QUnit.test('_set can rise the dirty flag', function(assert) { + var object = new fabric.Object({ fill: 'blue' }); + object.dirty = false; + object._set('fill', 'red'); + assert.equal(object.dirty, true, 'dirty is rised'); + }); + QUnit.test('_set rise dirty flag only if value changed', function(assert) { + var object = new fabric.Object({ fill: 'blue' }); + object.dirty = false; + object._set('fill', 'blue'); + assert.equal(object.dirty, false, 'dirty is not rised'); + }); })();