From 484b2b0c94501b6c921f531ec99902ee6fc49c2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Tue, 8 Mar 2011 17:20:03 +0000 Subject: [PATCH] Test code for Path#segments change observation. --- src/path/Path.js | 69 ++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/src/path/Path.js b/src/path/Path.js index 152f7755..a47812a8 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -27,37 +27,6 @@ var Path = this.Path = PathItem.extend({ || typeof segments[0] != 'object' ? arguments : segments); }, - /** - * The segments contained within the path. - */ - getSegments: function() { - return this._segments; - }, - - setSegments: function(segments) { - var length = segments.length; - if (!this._segments) { - this._segments = new Array(length); - /* - this._segments = Base.each( - ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], - function(name) { - var prev = this[name]; - this[name] = function() { - return prev.apply(this, arguments); - } - }, - new Array(length) - ); - */ - } else { - this._segments.length = length; - } - for(var i = 0; i < length; i++) { - this._add(Segment.read(segments, i, 1)); - } - }, - /** * The curves contained within the path. */ @@ -200,6 +169,44 @@ var Path = this.Path = PathItem.extend({ ctx.restore(); } } +}, new function() { // Scope for segments list change detection + + var segmentsFields = Base.each( + ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], + function(name) { + var func = Array.prototype[name]; + this[name] = function() { + return func.apply(this, arguments); + } + }, {}); + + return { + beans: true, + + /** + * The segments contained within the path. + */ + getSegments: function() { + return this._segments; + }, + + setSegments: function(segments) { + var length = segments.length; + if (!this._segments) { + // Enhance the _segments array with functions that watch for + // change. + this._segments = Base.each(segmentsFields, + function(value, name) { + this[name] = value; + }, []); + } else { + this._segments.length = 0; + } + for(var i = 0; i < length; i++) { + this._add(Segment.read(segments, i, 1)); + } + } + } }, new function() { // Inject methods that require scoped privates /**