Permalink
Browse files

Alternate method of handling first/lastObject changes

  • Loading branch information...
1 parent 7d9806c commit 9022e6087e074dd2ae82cf4a65e6a9116b0c4129 @wagenet wagenet committed May 4, 2012
@@ -329,16 +329,15 @@ Ember.Array = Ember.Mixin.create(Ember.Enumerable, /** @scope Ember.Array.protot
removing = removeAmt;
}
- var len = this._originalLength = get(this, 'length'),
- firstChanged = startIdx === 0,
- lastChanged = startIdx + removeAmt >= len;
-
this.enumerableContentWillChange(removing, addAmt);
- if (firstChanged) { Ember.propertyWillChange(this, 'firstObject'); }
- if (lastChanged) { Ember.propertyWillChange(this, 'lastObject'); }
// Make sure the @each proxy is set up if anyone is observing @each
if (Ember.isWatching(this, '@each')) { get(this, '@each'); }
+
+ // Make sure we've cached these for the check in arrayContentDidChange
+ get(this, 'firstObject');
+ get(this, 'lastObject');
+
return this;
},
@@ -362,14 +361,19 @@ Ember.Array = Ember.Mixin.create(Ember.Enumerable, /** @scope Ember.Array.protot
adding = addAmt;
}
- var len = this._originalLength,
- firstChanged = startIdx === 0,
- lastChanged = startIdx + removeAmt >= len;
-
this.enumerableContentDidChange(removeAmt, adding);
- if (firstChanged) { Ember.propertyDidChange(this, 'firstObject'); }
- if (lastChanged) { Ember.propertyDidChange(this, 'lastObject'); }
Ember.sendEvent(this, '@array:change', startIdx, removeAmt, addAmt);
+
+ var length = get(this, 'length');
+ if (this.objectAt(0) !== get(this, 'firstObject')) {
+ Ember.propertyWillChange(this, 'firstObject');
+ Ember.propertyDidChange(this, 'firstObject');
+ }
+ if (this.objectAt(length-1) !== get(this, 'lastObject')) {
+ Ember.propertyWillChange(this, 'lastObject');
+ Ember.propertyDidChange(this, 'lastObject');
+ }
+
return this;
},
@@ -77,8 +77,7 @@ suite.test("[A,B,C].unshiftObject(A) => [A,A,B,C] + notify", function() {
equal(observer.timesCalled('[]'), 1, 'should have notified [] once');
equal(observer.timesCalled('@each'), 1, 'should have notified @each once');
equal(observer.timesCalled('length'), 1, 'should have notified length once');
- // Ideally we would not notify this, but the way observers are currently it's hard to avoid
- equal(observer.timesCalled('firstObject'), 1, 'should have notified firstObject once');
+ equal(observer.validate('firstObject'), false, 'should NOT have notified firstObject');
equal(observer.validate('lastObject'), false, 'should NOT have notified lastObject');
});
@@ -75,8 +75,7 @@ suite.test("[A,B,C].unshiftObjects([A,B]) => [A,B,A,B,C] + notify", function() {
equal(observer.timesCalled('[]'), 1, 'should have notified [] once');
equal(observer.timesCalled('@each'), 1, 'should have notified @each once');
equal(observer.timesCalled('length'), 1, 'should have notified length once');
- // Technically we shouldn't notify of a change here, but this is hard to avoid
- equal(observer.timesCalled('firstObject'), 1, 'should have notified firstObject once');
+ equal(observer.validate('firstObject'), false, 'should NOT have notified firstObject');
equal(observer.validate('lastObject'), false, 'should NOT have notified lastObject');
});

0 comments on commit 9022e60

Please sign in to comment.