reworked _getCache implementation

This commit is contained in:
Eric Rowell
2013-08-09 22:31:25 -07:00
parent 3933568b45
commit 41dea5300a

View File

@@ -46,10 +46,6 @@
skewY: TRANSFORM skewY: TRANSFORM
}; };
CACHE_ATTRS = {
transform: 1
};
Kinetic.Util.addMethods(Kinetic.Node, { Kinetic.Util.addMethods(Kinetic.Node, {
_init: function(config) { _init: function(config) {
this._id = Kinetic.Global.idCounter++; this._id = Kinetic.Global.idCounter++;
@@ -64,24 +60,15 @@
delete this.cache[cacheAttr]; delete this.cache[cacheAttr];
} }
}, },
_getCache: function(attr, def){ _getCache: function(attr, privateGetter){
var val, cache; var cache = this.cache[attr];
// if attr is cacheable // if not cached, we need to set it using the private getter method.
if (CACHE_ATTRS[attr]) { if (!cache) {
cache = this.cache[attr]; this.cache[attr] = privateGetter.call(this);
// if not cached, we need to set it using the private getter method.
if (!cache) {
this.cache[attr] = this[PRIVATE_GET + Kinetic.Util._capitalize(attr)]();
}
return this.cache[attr];
}
else {
val = this.attrs[attr];
return val === undefined ? def : val;
} }
return this.cache[attr];
}, },
/** /**
* bind events to the node. KineticJS supports mouseover, mousemove, * bind events to the node. KineticJS supports mouseover, mousemove,
@@ -1148,6 +1135,14 @@
}, },
isDraggable: function() { isDraggable: function() {
return false; return false;
},
/**
* get transform of the node
* @method
* @memberof Kinetic.Node.prototype
*/
getTransform: function() {
return this._getCache(TRANSFORM, this._getTransform);
} }
}); });
@@ -1221,7 +1216,8 @@
method = GET + Kinetic.Util._capitalize(attr); method = GET + Kinetic.Util._capitalize(attr);
constructor.prototype[method] = function() { constructor.prototype[method] = function() {
return this._getCache(attr, def); var val = this.attrs[attr];
return val === undefined ? def : val;
}; };
}; };
Kinetic.Node.addPointGetter = function(constructor, attr) { Kinetic.Node.addPointGetter = function(constructor, attr) {
@@ -1727,14 +1723,6 @@
* @param {Boolean} visible * @param {Boolean} visible
*/ */
Kinetic.Node.addGetter(Kinetic.Node, 'transform');
/**
* get transform of the node
* name getTransform
* @method
* @memberof Kinetic.Node.prototype
*/
// aliases // aliases
/** /**