Skip to content

Commit 864b46a

Browse files
committed
fix(collectionRepeat): restore scrollView's normal behavior when repeater is destroyed
Closes #2078.
1 parent dcac56a commit 864b46a

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

js/angular/directive/collectionRepeat.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,21 @@ function RepeatManagerFactory($rootScope, $window, $$rAF) {
406406
(isGridView ? GridViewType : ListViewType).call(view);
407407
(isStaticView ? StaticViewType : DynamicViewType).call(view);
408408

409+
var contentSizeStr = isVertical ? 'getContentHeight' : 'getContentWidth';
410+
var originalGetContentSize = scrollView.options[contentSizeStr];
411+
scrollView.options[contentSizeStr] = angular.bind(view, view.getContentSize);
412+
413+
scrollView.__$callback = scrollView.__callback;
414+
scrollView.__callback = function(transformLeft, transformTop, zoom, wasResize) {
415+
var scrollValue = view.getScrollValue();
416+
if (renderStartIndex === -1 ||
417+
scrollValue + view.scrollPrimarySize > renderAfterBoundary ||
418+
scrollValue < renderBeforeBoundary) {
419+
render();
420+
}
421+
scrollView.__$callback(transformLeft, transformTop, zoom, wasResize);
422+
};
423+
409424
var isLayoutReady = false;
410425
var isDataReady = false;
411426
this.refreshLayout = function(itemsAfterRepeater) {
@@ -448,6 +463,8 @@ function RepeatManagerFactory($rootScope, $window, $$rAF) {
448463
}
449464
};
450465

466+
467+
451468
this.refreshData = function(newData) {
452469
newData || (newData = []);
453470

@@ -471,32 +488,21 @@ function RepeatManagerFactory($rootScope, $window, $$rAF) {
471488
render.destroyed = true;
472489
unwatch();
473490

474-
scrollView.__calback = scrollView.__$callback;
475491
itemsPool.forEach(function(item) {
476492
item.scope.$destroy();
477493
item.scope = item.element = item.node = item.images = null;
478494
});
479495
itemsPool.length = itemsEntering.length = itemsLeaving.length = 0;
480496
itemsShownMap = {};
481497

482-
(view.onDestroy || angular.noop)();
483-
};
498+
//Restore the scrollView's normal behavior and resize it to normal size.
499+
scrollView.options[contentSizeStr] = originalGetContentSize;
500+
scrollView.__callback = scrollView.__$callback;
501+
scrollView.resize();
484502

485-
scrollView.options[isVertical ? 'getContentHeight' : 'getContentWidth'] =
486-
angular.bind(view, view.getContentSize);
487-
488-
scrollView.__$callback = scrollView.__callback;
489-
scrollView.__callback = function(transformLeft, transformTop, zoom, wasResize) {
490-
var scrollValue = view.getScrollValue();
491-
if (renderStartIndex === -1 ||
492-
scrollValue + view.scrollPrimarySize > renderAfterBoundary ||
493-
scrollValue < renderBeforeBoundary) {
494-
render();
495-
}
496-
scrollView.__$callback(transformLeft, transformTop, zoom, wasResize);
503+
(view.onDestroy || angular.noop)();
497504
};
498505

499-
500506
function forceRerender() {
501507
return render(true);
502508
}

test/unit/angular/directive/collectionRepeat.unit.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,30 @@ describe('collectionRepeat', function() {
156156
}).toThrow();
157157
}));
158158

159-
it('should destroy', inject(function($compile, $rootScope) {
159+
it('should destroy and restore normal scrollView behavior', inject(function($compile, $rootScope) {
160160
var scope = $rootScope.$new();
161-
var content = $compile('<ion-content>' +
162-
'<div collection-repeat="item in items" item-height="5" item-width="5"></div>' +
163-
'</ion-content>')(scope);
161+
var content = $compile('<ion-content>')(scope);
162+
var scrollView = content.data('$$ionicScrollController').scrollView;
163+
164+
var originalCallback = scrollView.__callback;
165+
var originalGetContentHeight = scrollView.options.getContentHeight;
166+
167+
var repeater = angular.element(
168+
'<div collection-repeat="item in items" item-height="5" item-width="5"></div>'
169+
);
170+
content.append(repeater);
171+
$compile(repeater)(content.scope());
164172
$rootScope.$apply();
165173
content.triggerHandler('scroll.init');
174+
$rootScope.$apply();
175+
176+
expect(scrollView.__callback).not.toBe(originalCallback);
177+
expect(scrollView.options.getContentHeight).not.toBe(originalGetContentHeight);
178+
166179
scope.$destroy();
180+
181+
expect(scrollView.__callback).toBe(originalCallback);
182+
expect(scrollView.options.getContentHeight).toBe(originalGetContentHeight);
167183
}));
168184

169185
describe('automatic dimensions', function() {

0 commit comments

Comments
 (0)