Skip to content

Commit

Permalink
Make generic _set aware of real change property (#4415)
Browse files Browse the repository at this point in the history
* first change

* added a small test
  • Loading branch information
asturur authored Oct 26, 2017
1 parent c678704 commit 141f60a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down
18 changes: 18 additions & 0 deletions test/unit/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
})();

0 comments on commit 141f60a

Please sign in to comment.