Skip to content

Commit

Permalink
Rewrite cell update logic as a CP; remove array observers
Browse files Browse the repository at this point in the history
  • Loading branch information
mmun committed Aug 18, 2015
1 parent ccdc0c5 commit 15aa798
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions addon/components/ember-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export default Ember.Component.extend({
// this.lastCell = undefined;
// this.cellCount = undefined;
this.contentElement = undefined;
this.cells = [];
this.cellMap = Object.create(null);
this._cells = [];
this._cellMap = Object.create(null);

// TODO: Super calls should always be at the top of the constructor.
// I had to move the super call after the properties were defined to
Expand Down Expand Up @@ -76,13 +76,7 @@ export default Ember.Component.extend({
var calculateSize = false;

if (this._cellLayout !== cellLayout || this._items !== items) {
if (this._items != null && this._items !== items ) {
this._items.removeArrayObserver(this);
}
this._items = items;
if (this._items != null) {
this._items.addArrayObserver(this);
}
this.set('_items', items);
this._cellLayout = cellLayout;
calculateSize = true;
}
Expand All @@ -96,10 +90,7 @@ export default Ember.Component.extend({
Ember.run.scheduleOnce('afterRender', this, 'calculateContentSize');
}
},
arrayWillChange() { },
arrayDidChange() {
this.rerender();
},

didInsertElement() {
this._super();
this.contentElement = this.element.firstElementChild;
Expand Down Expand Up @@ -135,12 +126,11 @@ export default Ember.Component.extend({
// cancelable and it should be canceled inside of willDestroyElement.
callback();
},
willDestroyElement() {
this._super();
if (this._items != null) {
this._items.removeArrayObserver(this);
}
},

cells: Ember.computed('_items.[]', function() {
return this.updateCells();
}),

setupScroller() {
//this.element.addEventListener('scroll', Ember.run.bind(this, 'updateOffset'));
// TODO save for teardown
Expand All @@ -153,14 +143,14 @@ export default Ember.Component.extend({
// this.rerender();
// }
// },
willRender() {
updateCells() {
if (!this._items) { return; }
if (this._cellLayout.length !== this._items.length) {
this._cellLayout.length = this._items.length;
this.calculateContentSize();
}

var priorMap = this.cellMap;
var priorMap = this._cellMap;
var cellMap = Object.create(null);

var index = this._cellLayout.indexAt(this._offsetX, this._offsetY, this._width, this._height);
Expand Down Expand Up @@ -192,8 +182,8 @@ export default Ember.Component.extend({
}
}

for (i=0; i<this.cells.length; i++) {
cell = this.cells[i];
for (i=0; i<this._cells.length; i++) {
cell = this._cells[i];
if (!cellMap[cell.key]) {
if (newItems.length) {
itemIndex = newItems.pop();
Expand Down Expand Up @@ -224,9 +214,10 @@ export default Ember.Component.extend({
style = formatStyle(pos, width, height);
cell = new Cell(itemKey, items[itemIndex], itemIndex, style);
cellMap[itemKey] = cell;
this.cells.pushObject(cell);
this._cells.pushObject(cell);
}
this.cellMap = cellMap;
this._cellMap = cellMap;
return this._cells;
},
calculateBounds() {
// make sure rendered before accessing style.
Expand Down

0 comments on commit 15aa798

Please sign in to comment.