From 3e6b88008642b02ed1d2c23c242accce13365476 Mon Sep 17 00:00:00 2001 From: kangax Date: Tue, 7 Aug 2012 22:41:00 +0200 Subject: [PATCH] Fix `set({ key1: value1, key2: value2 })` not working with groups. --- src/group.class.js | 30 +++++++++--------------------- src/object.class.js | 43 +++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/group.class.js b/src/group.class.js index e4582cc8..2399ccfb 100644 --- a/src/group.class.js +++ b/src/group.class.js @@ -161,31 +161,19 @@ }, /** - * Sets property to a given value - * @method set - * @param {String} name - * @param {Object|Function} value - * @return {fabric.Group} thisArg - * @chainable + * @private */ - set: function(name, value) { - if (typeof value == 'function') { - // recurse - this.set(name, value(this[name])); + _set: function(key, value) { + if (key === 'fill' || key === 'opacity') { + var i = this.objects.length; + this[key] = value; + while (i--) { + this.objects[i].set(key, value); + } } else { - if (name === 'fill' || name === 'opacity') { - var i = this.objects.length; - this[name] = value; - while (i--) { - this.objects[i].set(name, value); - } - } - else { - this[name] = value; - } + this[key] = value; } - return this; }, /** diff --git a/src/object.class.js b/src/object.class.js index 35832b9a..52f1d365 100644 --- a/src/object.class.js +++ b/src/object.class.js @@ -281,34 +281,45 @@ }, /** - * Basic setter - * @param {Any} property - * @param {Any} value - * @return {fabric.Object} thisArg + * Sets property to a given value + * @method set + * @param {String} name + * @param {Object|Function} value + * @return {fabric.Group} thisArg * @chainable */ - set: function(property, value) { - var shouldConstrainValue = (property === 'scaleX' || property === 'scaleY') && value < this.MIN_SCALE_LIMIT; - if (shouldConstrainValue) { - value = this.MIN_SCALE_LIMIT; - } - if (typeof property == 'object') { - for (var prop in property) { - this.set(prop, property[prop]); + set: function(key, value) { + if (typeof key === 'object') { + for (var prop in key) { + this._set(prop, key[prop]); } } else { - if (property === 'angle') { - this.setAngle(value); + if (typeof value === 'function') { + this._set(key, value(this.get(key))); } else { - this[property] = value; + this._set(key, value); } } - return this; }, + _set: function(key, value) { + var shouldConstrainValue = (key === 'scaleX' || key === 'scaleY') && + value < this.MIN_SCALE_LIMIT; + + if (shouldConstrainValue) { + value = this.MIN_SCALE_LIMIT; + } + if (key === 'angle') { + this.setAngle(value); + } + else { + this[key] = value; + } + }, + /** * Toggles specified property from `true` to `false` or from `false` to `true` * @method toggle