Skip to content

Instantly share code, notes, and snippets.

@michaelmr
Last active July 15, 2016 20:38
Show Gist options
  • Save michaelmr/46e22138e72cc2981d5c9b5c858e690d to your computer and use it in GitHub Desktop.
Save michaelmr/46e22138e72cc2981d5c9b5c858e690d to your computer and use it in GitHub Desktop.
angular.module("groupmuse").service("BackButtonManager", function($rootScope, $ionicPlatform) {
var managedStates;
managedStates = [];
$rootScope.$on('$stateChangeSuccess', function(event, next) {
var state, _i, _j, _len, _len1, _results;
for (_i = 0, _len = managedStates.length; _i < _len; _i++) {
state = managedStates[_i];
if (state.enabled) {
state.unregisterCallback();
state.enabled = false;
}
}
_results = [];
for (_j = 0, _len1 = managedStates.length; _j < _len1; _j++) {
state = managedStates[_j];
if (next.name === state.name && !state.enabled) {
state.unregisterCallback = $ionicPlatform.registerBackButtonAction(state.callback, 100);
state.enabled = true;
break;
} else {
_results.push(void 0);
}
}
return _results;
});
return {
attachToState: function(state, callback) {
managedStates = _.reject(managedStates, function(s) {
return s.name === state;
});
return managedStates.push({
name: state,
callback: callback,
enabled: false
});
}
};
});
angular.module("your_app", ["ionic"]).run(function(BackButtonManager) {
return BackButtonManager.attachToState('app.events', function() {
var _ref, _ref1;
return ((_ref = navigator.app) != null ? _ref.exitApp() : void 0) || ((_ref1 = navigator.device) != null ? _ref1.exitApp() : void 0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment