Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(collectionRepeat): restore scrollView's normal behavior when repe…
Browse files Browse the repository at this point in the history
…ater is destroyed

Closes #2078.
ajoslin committed Feb 26, 2015
1 parent dcac56a commit 864b46a
Showing 2 changed files with 42 additions and 20 deletions.
38 changes: 22 additions & 16 deletions js/angular/directive/collectionRepeat.js
Original file line number Diff line number Diff line change
@@ -406,6 +406,21 @@ function RepeatManagerFactory($rootScope, $window, $$rAF) {
(isGridView ? GridViewType : ListViewType).call(view);
(isStaticView ? StaticViewType : DynamicViewType).call(view);

var contentSizeStr = isVertical ? 'getContentHeight' : 'getContentWidth';
var originalGetContentSize = scrollView.options[contentSizeStr];
scrollView.options[contentSizeStr] = angular.bind(view, view.getContentSize);

scrollView.__$callback = scrollView.__callback;
scrollView.__callback = function(transformLeft, transformTop, zoom, wasResize) {
var scrollValue = view.getScrollValue();
if (renderStartIndex === -1 ||
scrollValue + view.scrollPrimarySize > renderAfterBoundary ||
scrollValue < renderBeforeBoundary) {
render();
}
scrollView.__$callback(transformLeft, transformTop, zoom, wasResize);
};

var isLayoutReady = false;
var isDataReady = false;
this.refreshLayout = function(itemsAfterRepeater) {
@@ -448,6 +463,8 @@ function RepeatManagerFactory($rootScope, $window, $$rAF) {
}
};



this.refreshData = function(newData) {
newData || (newData = []);

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

scrollView.__calback = scrollView.__$callback;
itemsPool.forEach(function(item) {
item.scope.$destroy();
item.scope = item.element = item.node = item.images = null;
});
itemsPool.length = itemsEntering.length = itemsLeaving.length = 0;
itemsShownMap = {};

(view.onDestroy || angular.noop)();
};
//Restore the scrollView's normal behavior and resize it to normal size.
scrollView.options[contentSizeStr] = originalGetContentSize;
scrollView.__callback = scrollView.__$callback;
scrollView.resize();

scrollView.options[isVertical ? 'getContentHeight' : 'getContentWidth'] =
angular.bind(view, view.getContentSize);

scrollView.__$callback = scrollView.__callback;
scrollView.__callback = function(transformLeft, transformTop, zoom, wasResize) {
var scrollValue = view.getScrollValue();
if (renderStartIndex === -1 ||
scrollValue + view.scrollPrimarySize > renderAfterBoundary ||
scrollValue < renderBeforeBoundary) {
render();
}
scrollView.__$callback(transformLeft, transformTop, zoom, wasResize);
(view.onDestroy || angular.noop)();
};


function forceRerender() {
return render(true);
}
24 changes: 20 additions & 4 deletions test/unit/angular/directive/collectionRepeat.unit.js
Original file line number Diff line number Diff line change
@@ -156,14 +156,30 @@ describe('collectionRepeat', function() {
}).toThrow();
}));

it('should destroy', inject(function($compile, $rootScope) {
it('should destroy and restore normal scrollView behavior', inject(function($compile, $rootScope) {
var scope = $rootScope.$new();
var content = $compile('<ion-content>' +
'<div collection-repeat="item in items" item-height="5" item-width="5"></div>' +
'</ion-content>')(scope);
var content = $compile('<ion-content>')(scope);
var scrollView = content.data('$$ionicScrollController').scrollView;

var originalCallback = scrollView.__callback;
var originalGetContentHeight = scrollView.options.getContentHeight;

var repeater = angular.element(
'<div collection-repeat="item in items" item-height="5" item-width="5"></div>'
);
content.append(repeater);
$compile(repeater)(content.scope());
$rootScope.$apply();
content.triggerHandler('scroll.init');
$rootScope.$apply();

expect(scrollView.__callback).not.toBe(originalCallback);
expect(scrollView.options.getContentHeight).not.toBe(originalGetContentHeight);

scope.$destroy();

expect(scrollView.__callback).toBe(originalCallback);
expect(scrollView.options.getContentHeight).toBe(originalGetContentHeight);
}));

describe('automatic dimensions', function() {

0 comments on commit 864b46a

Please sign in to comment.