From ee1d5d9ce88cef3fe85810963cebd4b7c65ae6b3 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 4 Dec 2014 13:28:41 -0700 Subject: [PATCH] fix(slideBox): if selected binding is string, parse to integer --- js/angular/controller/slideBoxController.js | 2 ++ test/unit/angular/directive/slideBox.unit.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/js/angular/controller/slideBoxController.js b/js/angular/controller/slideBoxController.js index 03f3745331..98043640bb 100644 --- a/js/angular/controller/slideBoxController.js +++ b/js/angular/controller/slideBoxController.js @@ -178,6 +178,8 @@ function(scope, element, $log, $document, $$q, $timeout, $interval, $$ionicAttac // adds data to the queue for selection. // Index can be either a number or a getter (to be called when starting the slide) function select(newIndex, transitionDuration, isDrag) { + newIndex = parseInt(newIndex); + if (isNaN(newIndex) || newIndex < 0) return; slideQueue.unshift([ angular.isFunction(newIndex) ? newIndex : function() { return newIndex; }, transitionDuration || SLIDE_TRANSITION_DURATION, diff --git a/test/unit/angular/directive/slideBox.unit.js b/test/unit/angular/directive/slideBox.unit.js index 6bf3fb22f0..5e077d259b 100644 --- a/test/unit/angular/directive/slideBox.unit.js +++ b/test/unit/angular/directive/slideBox.unit.js @@ -340,6 +340,24 @@ describe('ionSlideBox', function() { expect(slideDisplays(el)).toEqual(['selected', 'next', 'previous']); })); + it('strings and negative numbers', inject(function($timeout, $rootScope) { + var el = setup(); + $timeout.flush(); + + // Do nothing for -1 + $del.select(-1); + $timeout.verifyNoPendingTasks(); + + // Do nothing for NaN + $del.select(NaN); + $timeout.verifyNoPendingTasks(); + + // parse to int + $del.select('1'); + $timeout.flush(); + expect($del.selected()).toBe(1); + })); + it('when queueing, should only publish after final slide', inject(function($timeout, $rootScope) { $rootScope.changed = jasmine.createSpy('changed'); $rootScope.start = jasmine.createSpy('start');