Permalink
Browse files

Added fix + unit test for range observers when calling SC.SparseArray…

… delegate methods.
  • Loading branch information...
1 parent 2145206 commit 45a665ba5cc19f8ac1f36809062f476d898d26d6 Tom Dale committed Apr 21, 2011
@@ -73,10 +73,12 @@ SC.SparseArray = SC.Object.extend(SC.Observable, SC.Enumerable, SC.Array,
@returns {SC.SparseArray} receiver
*/
provideLength: function(length) {
+ var oldLength;
if (SC.none(length)) this._sa_content = null ;
if (length !== this._length) {
+ oldLength = this._length;
this._length = length ;
- if (this._requestingLength <= 0) this.enumerableContentDidChange() ;
+ if (this._requestingLength <= 0) { this.arrayContentDidChange(0, oldLength||0, length||0) ; }
}
return this ;
},
@@ -256,7 +258,7 @@ SC.SparseArray = SC.Object.extend(SC.Observable, SC.Enumerable, SC.Array,
if (!content) content = this._sa_content = [] ;
var start = range.start, len = range.length;
while(--len >= 0) content[start+len] = array.objectAt(len);
- if (this._requestingIndex <= 0) this.enumerableContentDidChange(range.start, range.length);
+ if (this._requestingIndex <= 0) this.arrayContentDidChange(range.start, range.length, range.length);
return this ;
},
@@ -299,7 +301,7 @@ SC.SparseArray = SC.Object.extend(SC.Observable, SC.Enumerable, SC.Array,
while (--loc>=start) content[loc] = undefined;
}
}
- this.enumerableContentDidChange(range.start, range.length) ; // notify
+ this.arrayContentDidChange(range.start, range.length, range.length) ; // notify
return this ;
},
@@ -376,9 +378,11 @@ SC.SparseArray = SC.Object.extend(SC.Observable, SC.Enumerable, SC.Array,
@returns {SC.SparseArray} receiver
*/
reset: function() {
+ var oldLength;
this._sa_content = null ;
+ oldLength = this._length;
this._length = null ;
- this.enumerableContentDidChange() ;
+ this.arrayContentDidChange(0, oldLength, 0);
this.invokeDelegateMethod(this.delegate, 'sparseArrayDidReset', this);
return this ;
}
@@ -199,3 +199,34 @@ SC.ArraySuite.generate("SC.SparseArray", {
return SC.SparseArray.create({ delegate: del });
}
});
+
+test("should notify enumerable property", function() {
+ var arr = SC.SparseArray.create();
+ var count = 0;
+ function counter() {
+ count++;
+ }
+
+ arr.provideLength(1);
+ arr.addObserver('[]', this, counter);
+ arr.provideObjectAtIndex(0, 'one');
+ equals(count, 1, "observer should have fired once");
+});
+
+test("should notify range observers", function() {
+ var arr = SC.SparseArray.create();
+ var count = 0;
+ function counter(arr, objects, key, indexes) {
+ count++;
+ }
+
+ arr.provideLength(4);
+ var is = SC.IndexSet.create(0,1).add(2,1);
+ arr.addRangeObserver(is, this, counter);
+ arr.provideObjectAtIndex(0, 'one');
+ arr.provideObjectAtIndex(1, 'two');
+ arr.provideObjectAtIndex(2, 'three');
+ arr.provideObjectAtIndex(3, 'four');
+ equals(2, count, "observer should have fired twice");
+});
+

0 comments on commit 45a665b

Please sign in to comment.