refactor(content): set saved-scroll after timeout, only set valid values

Addresses #439
This commit is contained in:
Andy Joslin
2014-02-08 00:17:35 -05:00
parent 7f9bfb5a94
commit 8581d162a3
2 changed files with 19 additions and 6 deletions

View File

@@ -97,8 +97,11 @@ angular.module('ionic.ui.content', ['ionic.ui.service', 'ionic.ui.scroll'])
$scope.$on('$viewContentLoaded', function(e, viewHistoryData) {
viewHistoryData || (viewHistoryData = {});
if (viewHistoryData.scrollValues) {
scrollView.scrollTo(viewHistoryData.scrollValues);
var scroll = viewHistoryData.scrollValues;
if (scroll) {
$timeout(function() {
scrollView.scrollTo(+scroll.left || null, +scroll.top || null);
}, 0);
}
//Save scroll onto viewHistoryData when scope is destroyed

View File

@@ -1,6 +1,6 @@
describe('Ionic Content directive', function() {
var compile, element, scope;
beforeEach(module('ionic.ui.content'));
beforeEach(inject(function($compile, $rootScope, $timeout, $window) {
@@ -10,7 +10,7 @@ describe('Ionic Content directive', function() {
window = $window;
ionic.Platform.setPlatform('Android');
}));
it('Has content class', function() {
element = compile('<content></content>')(scope);
expect(element.hasClass('scroll-content')).toBe(true);
@@ -91,10 +91,20 @@ describe('Ionic Content directive', function() {
compileWithParent();
spyOn(scope.scrollView, 'scrollTo');
var scrollValues = { top: 40, left: -20, zoom: 3 };
scope.$broadcast('$viewContentLoaded', {
scope.$broadcast('$viewContentLoaded', {
scrollValues: scrollValues
});
expect(scope.scrollView.scrollTo).toHaveBeenCalledWith(scrollValues);
timeout.flush();
expect(scope.scrollView.scrollTo.mostRecentCall.args).toEqual([-20, 40]);
});
it('should set null with historyData.scrollValues not valid', function() {
compileWithParent();
spyOn(scope.scrollView, 'scrollTo');
var scrollValues = { left: 'bar', top: 'foo' };
scope.$broadcast('$viewContentLoaded', { scrollValues: scrollValues });
timeout.flush();
expect(scope.scrollView.scrollTo.mostRecentCall.args).toEqual([null, null]);
});
it('should save scroll on the historyData passed in on $destroy', function() {