2014-04-24 5 views
3

ionicframework와 cordova를 사용하여 수행되는 응용 프로그램이 있습니다. 내 애플 리케이션에서는 사용자가 다시 버튼을 누른 다음 그것을 무시해야 할 경우 요구 사항이 있습니다. 그러나 사용자가 세 번째로 누르면 응용 프로그램을 종료해야합니다.코르도바 backbutton preventDefault가 작동하지 않습니다

이전에 프로젝트는 phonegap 및 jquery를 사용하여 완료되었으며 동일한 코드가 작동합니다. 나는 작은 예외를 던졌을 때 예외를 던지면 애플 리케이션이 닫히지 않아야했다.

document.addEventListener("backbutton", function (e) { 
     if (new Date() - firstDateClick > 1000) { 
      firstDateClick = new Date(); 
      totalClicks = 1; 
     } else { 
      totalClicks++; 
      if (totalClicks >= 3) { 
       var answer = confirm('Are You Sure You Want Exit'); 
       if (answer) { 
        var service = angular.injector(['ng', 'starter.services']).get('DanceService'); 
        service.logEvent("exit") 
         .then(function() { 
          alert('exit1') 
          if (navigator.app) { 
           navigator.app.exitApp(); 
          } 
          else if (navigator.device) { 
           navigator.device.exitApp(); 
          } 

         }) 
       } else { 
        totalClicks = 1; 
       } 
      } 
     } 
     throw "ignore" 
    }); 

하지만 예외를 포기하는 것이 좋습니다.

+0

을하는 데 도움이 당신이 내가 단지 게시 한 @jcesar에서 preventDefault – jcesarmobile

+0

을 사용하는 경우 표시되지 않습니다 해결 방법 ... –

+0

어쨌든 backbutton 이벤트를 수신하고 관리하지 않는 기능을 만들면 아무 것도 방지 할 필요가 없습니다. 함수에 세 번째 매개 변수를 추가하십시오. document.addEventListener ("backbutton", yourCallbackFunction, false); – jcesarmobile

답변

0

앱에서 로그 아웃하기 전에 두 번 제어했습니다. 따라서 사용자는 한 번 누를 수 있으며 "다시 누르면 종료"라는 텍스트와 건배가 표시되고 두 번째 누르면 -> 로그 아웃됩니다.

var deregisterFunction = null; 

    return { 
     disableBack: disableBack, 
     registerAction: registerAction, 
     goHome: goHome, 
     goBack: goBack, 
     deregisterAction: deregisterAction, 
     closeApp: closeApp 
    }; 

    function disableBack() { 
     deregisterFunction = angular.copy($ionicPlatform.registerBackButtonAction(null, 101)); 
    } 

    function registerAction(cb, priority) { 
     deregisterFunction = angular.copy($ionicPlatform.registerBackButtonAction(cb, priority)); 
    } 

    function deregisterAction() { 
     if (deregisterFunction) { 
      deregisterFunction(); 
     } 
     goBack();//default behaviour 
    } 

    function goHome() { 
     deregisterFunction = angular.copy($ionicPlatform.registerBackButtonAction(function() { 
      $state.go('app.home'); 
     }, 101)); 
    } 

    function goBack() { 
     deregisterFunction = angular.copy($ionicPlatform.registerBackButtonAction(function() { 
      $ionicHistory.goBack(); 
     }, 101)); 
    } 

    function closeApp() { 
     deregisterFunction = angular.copy($ionicPlatform.registerBackButtonAction(function() { 
      ionic.Platform.exitApp(); 
     }, 101)); 
    } 

그리고이 내 유틸 서비스 :

내 서비스

var service = { 
     logoutToast: logoutToast, 
     resetToastCount: resetToastCount 
    }; 

    var toastCount = 0; 

    return service; 

    function resetToastCount() { 
     toastCount = 0; 
    } 

    function logoutToast() { 
     switch (toastCount) { 
     case 0: 
      $cordovaToast.show('Press again to log out', 'short', 'bottom'); 
      toastCount++; 
      break; 
     case 1: 
      toastCount = 0; 
      //logout 
      break; 
     default: 
      $cordovaToast.show('Error', 'short', 'bottom'); 
     } 
    } 

} 
}()); 

그래서 내 컨트롤러에서 나는 내 서비스의 작용 등록이 있습니다

$scope.$on('$ionicView.afterEnter', backButtonService.registerAction(utils.logoutToast, 101)); 

이것은 내가 원하는 곳에서 카운트를 재설정하기위한 것입니다 :

$scope.$on('$ionicView.afterEnter', utils.resetToastCount()); 

그리고 이것에 대한 전 탐색 할 때 내 조치를 등록 취소 :

$scope.$on('$stateChangeStart', backButtonService.deregisterAction); 

희망이 의지는 :

관련 문제