From db89558b850bd7e7b13102516083e9048145f9d7 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Thu, 25 Aug 2016 14:46:44 -0700 Subject: [PATCH] Fix #304 --- iron-list.html | 11 +++++------ test/basic.html | 9 +++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/iron-list.html b/iron-list.html index c7b70117..b47801b4 100644 --- a/iron-list.html +++ b/iron-list.html @@ -1453,9 +1453,11 @@ if (typeof idx !== 'number' || idx < 0 || idx > this.items.length - 1) { return; } - Polymer.dom.flush(); - + // Items should have been rendered prior scrolling to an index. + if (!this._itemsRendered) { + return; + } idx = Math.min(Math.max(idx, 0), this._virtualCount-1); // update the virtual start only when needed if (!this._isIndexRendered(idx) || idx >= this._maxVirtualStart) { @@ -1467,16 +1469,13 @@ this._assignModels(); // measure the new sizes this._updateMetrics(); - // estimate new physical offset - var estPhysicalTop = Math.floor(this._virtualStart / this._itemsPerRow) * this._physicalAverage; - this._physicalTop = estPhysicalTop; + this._physicalTop = Math.floor(this._virtualStart / this._itemsPerRow) * this._physicalAverage; var currentTopItem = this._physicalStart; var currentVirtualItem = this._virtualStart; var targetOffsetTop = 0; var hiddenContentSize = this._hiddenContentSize; - // scroll to the item as much as we can while (currentVirtualItem < idx && targetOffsetTop <= hiddenContentSize) { targetOffsetTop = targetOffsetTop + this._getPhysicalSizeIncrement(currentTopItem); diff --git a/test/basic.html b/test/basic.html index a0925a03..4c42e677 100644 --- a/test/basic.html +++ b/test/basic.html @@ -150,6 +150,15 @@ }, 100); }); + test('scroll to index while not attached', function() { + var tmpList = document.createElement('iron-list'); + Polymer.dom(tmpList).appendChild(document.createElement('template')); + tmpList.items = buildDataSet(100); + assert.equal(tmpList._virtualStart, 0); + tmpList.scrollToIndex(50); + assert.equal(tmpList._virtualStart, 0); + }); + test('scroll to item', function(done) { list.items = buildDataSet(100);