Debounce back button for nav controller

This commit is contained in:
Max Lynch
2013-11-11 21:29:55 -06:00
parent 56a64b8bc6
commit bae368ce79
2 changed files with 54 additions and 42 deletions

View File

@ -748,6 +748,31 @@ angular.module('ionic.ui.nav', ['ionic.service.templateLoad', 'ionic.service.ges
angular.extend(this, ionic.controllers.NavController.prototype);
/**
* Push a template onto the navigation stack.
* @param {string} templateUrl the URL of the template to load.
*/
this.pushFromTemplate = ionic.debounce(function(templateUrl) {
var childScope = $scope.$new();
childScope.isVisible = true;
// Load the given template
TemplateLoader.load(templateUrl).then(function(templateString) {
// Compile the template with the new scrope, and append it to the navigation's content area
var el = $compile(templateString)(childScope, function(cloned, scope) {
var content = angular.element($element[0].querySelector('.content, .scroll'));
$animate.enter(cloned, angular.element(content));
});
});
}, 300, true);
// Pop function, debounced
this.popController = ionic.debounce(function() {
_this.pop();
}, 300, true);
ionic.controllers.NavController.call(this, {
content: {
},
@ -771,7 +796,7 @@ angular.module('ionic.ui.nav', ['ionic.service.templateLoad', 'ionic.service.ges
// Support Android hardware back button (native only, not mobile web)
var onHardwareBackButton = function(e) {
$scope.$apply(function() {
_this.pop();
_this.popController();
});
}
Platform.onHardwareBackButton(onHardwareBackButton);
@ -784,25 +809,6 @@ angular.module('ionic.ui.nav', ['ionic.service.templateLoad', 'ionic.service.ges
this.endDrag = function(e) {
};
/**
* Push a template onto the navigation stack.
* @param {string} templateUrl the URL of the template to load.
*/
this.pushFromTemplate = ionic.debounce(function(templateUrl) {
var childScope = $scope.$new();
childScope.isVisible = true;
// Load the given template
TemplateLoader.load(templateUrl).then(function(templateString) {
// Compile the template with the new scrope, and append it to the navigation's content area
var el = $compile(templateString)(childScope, function(cloned, scope) {
var content = angular.element($element[0].querySelector('.content, .scroll'));
$animate.enter(cloned, angular.element(content));
});
});
}, 100, true);
/**
* Push a controller to the stack. This is called by the child
* nav-content directive when it is linked to a scope on the page.
@ -849,7 +855,7 @@ angular.module('ionic.ui.nav', ['ionic.service.templateLoad', 'ionic.service.ges
scope.$watch('navController.controllers.length', function(value) {
});
scope.goBack = function() {
navCtrl.pop();
navCtrl.popController();
};
}
};

View File

@ -8,6 +8,31 @@ angular.module('ionic.ui.nav', ['ionic.service.templateLoad', 'ionic.service.ges
angular.extend(this, ionic.controllers.NavController.prototype);
/**
* Push a template onto the navigation stack.
* @param {string} templateUrl the URL of the template to load.
*/
this.pushFromTemplate = ionic.debounce(function(templateUrl) {
var childScope = $scope.$new();
childScope.isVisible = true;
// Load the given template
TemplateLoader.load(templateUrl).then(function(templateString) {
// Compile the template with the new scrope, and append it to the navigation's content area
var el = $compile(templateString)(childScope, function(cloned, scope) {
var content = angular.element($element[0].querySelector('.content, .scroll'));
$animate.enter(cloned, angular.element(content));
});
});
}, 300, true);
// Pop function, debounced
this.popController = ionic.debounce(function() {
_this.pop();
}, 300, true);
ionic.controllers.NavController.call(this, {
content: {
},
@ -31,7 +56,7 @@ angular.module('ionic.ui.nav', ['ionic.service.templateLoad', 'ionic.service.ges
// Support Android hardware back button (native only, not mobile web)
var onHardwareBackButton = function(e) {
$scope.$apply(function() {
_this.pop();
_this.popController();
});
}
Platform.onHardwareBackButton(onHardwareBackButton);
@ -44,25 +69,6 @@ angular.module('ionic.ui.nav', ['ionic.service.templateLoad', 'ionic.service.ges
this.endDrag = function(e) {
};
/**
* Push a template onto the navigation stack.
* @param {string} templateUrl the URL of the template to load.
*/
this.pushFromTemplate = ionic.debounce(function(templateUrl) {
var childScope = $scope.$new();
childScope.isVisible = true;
// Load the given template
TemplateLoader.load(templateUrl).then(function(templateString) {
// Compile the template with the new scrope, and append it to the navigation's content area
var el = $compile(templateString)(childScope, function(cloned, scope) {
var content = angular.element($element[0].querySelector('.content, .scroll'));
$animate.enter(cloned, angular.element(content));
});
});
}, 100, true);
/**
* Push a controller to the stack. This is called by the child
* nav-content directive when it is linked to a scope on the page.
@ -109,7 +115,7 @@ angular.module('ionic.ui.nav', ['ionic.service.templateLoad', 'ionic.service.ges
scope.$watch('navController.controllers.length', function(value) {
});
scope.goBack = function() {
navCtrl.pop();
navCtrl.popController();
};
}
};