@@ -172,20 +172,9 @@ define(['exports', 'aurelia-dependency-injection', 'aurelia-binding', 'aurelia-t
172172
173173 VirtualRepeat . prototype . detached = function detached ( ) {
174174 this . scrollContainer . removeEventListener ( 'scroll' , this . scrollListener ) ;
175- this . _first = 0 ;
176- this . _previousFirst = 0 ;
177- this . _viewsLength = 0 ;
178- this . _lastRebind = 0 ;
179- this . _topBufferHeight = 0 ;
180- this . _bottomBufferHeight = 0 ;
181- this . _scrollingDown = false ;
182- this . _scrollingUp = false ;
183- this . _switchedDirection = false ;
175+ this . _resetCalculation ( ) ;
184176 this . _isAttached = false ;
185- this . _ticking = false ;
186- this . _hasCalculatedSizes = false ;
187177 this . templateStrategy . removeBufferElements ( this . element , this . topBuffer , this . bottomBuffer ) ;
188- this . isLastIndex = false ;
189178 this . scrollContainer = null ;
190179 this . scrollContainerHeight = null ;
191180 this . distanceToTop = null ;
@@ -210,40 +199,47 @@ define(['exports', 'aurelia-dependency-injection', 'aurelia-binding', 'aurelia-t
210199 var previousLastViewIndex = this . _getIndexOfLastView ( ) ;
211200
212201 var items = this . items ;
202+ var shouldCalculateSize = ! ! items ;
213203 this . strategy = this . strategyLocator . getStrategy ( items ) ;
214- if ( items . length > 0 && this . viewCount ( ) === 0 ) {
215- this . strategy . createFirstItem ( this ) ;
216- }
217204
218- if ( this . _itemsLength >= items . length ) {
219- this . _skipNextScrollHandle = true ;
220- reducingItems = true ;
205+ if ( shouldCalculateSize ) {
206+ if ( items . length > 0 && this . viewCount ( ) === 0 ) {
207+ this . strategy . createFirstItem ( this ) ;
208+ }
209+
210+ if ( this . _itemsLength >= items . length ) {
211+ this . _skipNextScrollHandle = true ;
212+ reducingItems = true ;
213+ }
214+ this . _checkFixedHeightContainer ( ) ;
215+ this . _calcInitialHeights ( items . length ) ;
221216 }
222- this . _checkFixedHeightContainer ( ) ;
223- this . _calcInitialHeights ( items . length ) ;
224217 if ( ! this . isOneTime && ! this . _observeInnerCollection ( ) ) {
225218 this . _observeCollection ( ) ;
226219 }
227220 this . strategy . instanceChanged ( this , items , this . _first ) ;
228- this . _lastRebind = this . _first ;
229221
230- if ( reducingItems && previousLastViewIndex > this . items . length - 1 ) {
231- if ( this . scrollContainer . tagName === 'TBODY' ) {
232- var realScrollContainer = this . scrollContainer . parentNode . parentNode ;
233- realScrollContainer . scrollTop = realScrollContainer . scrollTop + this . viewCount ( ) * this . itemHeight ;
234- } else {
235- this . scrollContainer . scrollTop = this . scrollContainer . scrollTop + this . viewCount ( ) * this . itemHeight ;
222+ if ( shouldCalculateSize ) {
223+ this . _lastRebind = this . _first ;
224+
225+ if ( reducingItems && previousLastViewIndex > this . items . length - 1 ) {
226+ if ( this . scrollContainer . tagName === 'TBODY' ) {
227+ var realScrollContainer = this . scrollContainer . parentNode . parentNode ;
228+ realScrollContainer . scrollTop = realScrollContainer . scrollTop + this . viewCount ( ) * this . itemHeight ;
229+ } else {
230+ this . scrollContainer . scrollTop = this . scrollContainer . scrollTop + this . viewCount ( ) * this . itemHeight ;
231+ }
236232 }
237- }
238- if ( ! reducingItems ) {
239- this . _previousFirst = this . _first ;
240- this . _scrollingDown = true ;
241- this . _scrollingUp = false ;
233+ if ( ! reducingItems ) {
234+ this . _previousFirst = this . _first ;
235+ this . _scrollingDown = true ;
236+ this . _scrollingUp = false ;
242237
243- this . isLastIndex = this . _getIndexOfLastView ( ) >= this . items . length - 1 ;
244- }
238+ this . isLastIndex = this . _getIndexOfLastView ( ) >= this . items . length - 1 ;
239+ }
245240
246- this . _handleScroll ( ) ;
241+ this . _handleScroll ( ) ;
242+ }
247243 } ;
248244
249245 VirtualRepeat . prototype . unbind = function unbind ( ) {
@@ -277,6 +273,24 @@ define(['exports', 'aurelia-dependency-injection', 'aurelia-binding', 'aurelia-t
277273 }
278274 } ;
279275
276+ VirtualRepeat . prototype . _resetCalculation = function _resetCalculation ( ) {
277+ this . _first = 0 ;
278+ this . _previousFirst = 0 ;
279+ this . _viewsLength = 0 ;
280+ this . _lastRebind = 0 ;
281+ this . _topBufferHeight = 0 ;
282+ this . _bottomBufferHeight = 0 ;
283+ this . _scrollingDown = false ;
284+ this . _scrollingUp = false ;
285+ this . _switchedDirection = false ;
286+ this . _ticking = false ;
287+ this . _hasCalculatedSizes = false ;
288+ this . _isAtTop = true ;
289+ this . isLastIndex = false ;
290+ this . elementsInView = 0 ;
291+ this . _adjustBufferHeights ( ) ;
292+ } ;
293+
280294 VirtualRepeat . prototype . _onScroll = function _onScroll ( ) {
281295 var _this4 = this ;
282296
@@ -300,6 +314,9 @@ define(['exports', 'aurelia-dependency-injection', 'aurelia-binding', 'aurelia-t
300314 this . _skipNextScrollHandle = false ;
301315 return ;
302316 }
317+ if ( ! this . items ) {
318+ return ;
319+ }
303320 var itemHeight = this . itemHeight ;
304321 var scrollTop = this . _fixedHeightContainer ? this . scrollContainer . scrollTop : pageYOffset - this . distanceToTop ;
305322 this . _first = Math . floor ( scrollTop / itemHeight ) ;
@@ -510,7 +527,12 @@ define(['exports', 'aurelia-dependency-injection', 'aurelia-binding', 'aurelia-t
510527 VirtualRepeat . prototype . _calcInitialHeights = function _calcInitialHeights ( itemsLength ) {
511528 var _this7 = this ;
512529
513- if ( this . _viewsLength > 0 && this . _itemsLength === itemsLength || this . _viewsLength > 0 && itemsLength < 0 ) {
530+ var isSameLength = this . _viewsLength > 0 && this . _itemsLength === itemsLength ;
531+ if ( isSameLength ) {
532+ return ;
533+ }
534+ if ( itemsLength < 1 ) {
535+ this . _resetCalculation ( ) ;
514536 return ;
515537 }
516538 this . _hasCalculatedSizes = true ;
0 commit comments