Skip to content

Commit

Permalink
fix(ionNavBackButton): stop flicker when pressing back on ios
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoslin committed Jun 11, 2014
1 parent c30be67 commit cec3a42
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 25 deletions.
3 changes: 1 addition & 2 deletions js/angular/controller/navBarController.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ function($scope, $element, $attrs, $ionicViewService, $animate, $compile, $ionic
$element[0].querySelector('.buttons.right-buttons')
);

this.back = function(e) {
this.back = function() {
var backView = $ionicViewService.getBackView();
backView && backView.go();
e && (e.alreadyHandled = true);
return false;
};

Expand Down
19 changes: 7 additions & 12 deletions js/angular/directive/navBackButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ IonicModule
'$animate',
'$rootScope',
'$sanitize',

'$ionicNavBarConfig',

function($animate, $rootScope, $sanitize, $ionicNavBarConfig) {
'$ionicNgClick',
function($animate, $rootScope, $sanitize, $ionicNavBarConfig, $ionicNgClick) {
var backIsShown = false;
//If the current viewstate does not allow a back button,
//always hide it.
Expand All @@ -80,23 +79,19 @@ function($animate, $rootScope, $sanitize, $ionicNavBarConfig) {
require: '^ionNavBar',
compile: function(tElement, tAttrs) {
tElement.addClass('button back-button ng-hide');

return function($scope, $element, $attr, navBarCtrl) {
console.log($attr.textFromTitle);

// Add a default back button icon based on the nav config, unless one is set
if($element[0].className.indexOf('ion-') < 0) {
$element.addClass($ionicNavBarConfig.backButtonIcon);
}

if (!$attr.ngClick) {
$scope.$navBack = navBarCtrl.back;
$element.on('click', function(event){
$scope.$apply(function() {
$scope.$navBack(event);
});
});
//Default to ngClick going back, but don't override a custom one
if (!isDefined($attr.ngClick)) {
$ionicNgClick($scope, $element, navBarCtrl.back);
}

//Make sure both that a backButton is allowed in the first place,
//and that it is shown by the current view.
$scope.$watch(function() {
Expand Down
4 changes: 3 additions & 1 deletion js/angular/directive/ngClick.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ IonicModule
*/
.factory('$ionicNgClick', ['$parse', function($parse) {
return function(scope, element, clickExpr) {
var clickHandler = $parse(clickExpr);
var clickHandler = angular.isFunction(clickExpr) ?
clickExpr :
$parse(clickExpr);

element.on('click', function(event) {
scope.$apply(function() {
Expand Down
Empty file added test/html/list-simple.html
Empty file.
9 changes: 4 additions & 5 deletions test/unit/angular/directive/navBackButton.unit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

describe('ionNavBackButton directive', function() {
beforeEach(module('ionic'));

Expand Down Expand Up @@ -62,11 +61,11 @@ describe('ionNavBackButton directive', function() {
expect(el.children().eq(0)[0].tagName.toLowerCase()).toBe('b');
});

it('should $navBack on click by default', function() {
it('should go back on click by default', function() {
var el = setup();
el.scope().$navBack = jasmine.createSpy('$navBack');
expect(el.controller('ionNavBar').back).not.toHaveBeenCalled();
el.triggerHandler('click');
expect(el.scope().$navBack).toHaveBeenCalled();
expect(el.controller('ionNavBar').back).toHaveBeenCalled();
});

it('should do ngClick expression if defined', function() {
Expand All @@ -80,7 +79,7 @@ describe('ionNavBackButton directive', function() {


describe('ionNavBackButton directive: Platforms', function() {
describe('ionNavBackButton directive: iOS Platform', function() {
describe('ionNavBackButton directive: iOS Platform', function() {
beforeEach(function($provide) {
TestUtil.setPlatform('ios');
});
Expand Down
8 changes: 3 additions & 5 deletions test/unit/angular/directive/navBar.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ describe('ionNavBar', function() {

it('should go back', inject(function($ionicViewService) {
var ctrl = setup();
var e = { alreadyHandled: false };
ctrl.back(e);
ctrl.back();
expect($ionicViewService.getBackView).toHaveBeenCalled();
expect(backView.go).toHaveBeenCalled();
expect(e.alreadyHandled).toBe(true);
}));

it('should align', function() {
Expand Down Expand Up @@ -308,7 +306,7 @@ describe('ionNavBar', function() {
describe('iOS', function() {
beforeEach(module('ionic', function($provide) {
TestUtil.setPlatform('ios');
$provide.constant('$ionicNavBarConfig', {
$provide.constant('$ionicNavBarConfig', {
alignTitle: 'center',
transition: 'nav-title-slide-ios7',
backButtonIcon: 'ion-ios7-arrow-back'
Expand All @@ -330,7 +328,7 @@ describe('ionNavBar', function() {
describe('Android', function() {
beforeEach(module('ionic', function($provide) {
TestUtil.setPlatform('android');
$provide.constant('$ionicNavBarConfig', {
$provide.constant('$ionicNavBarConfig', {
alignTitle: 'left',
transition: 'no-animation',
backButtonIcon: 'ion-android-back'
Expand Down

0 comments on commit cec3a42

Please sign in to comment.