Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add toPage argument to jQTouch.goBack() #406

Closed
wants to merge 1 commit into from

2 participants

@dhull

The jQTouch documentation at https://github.com/senchalabs/jQTouch/wiki/Public-Object says that the goBack function takes an optional argument that allows the caller to specify what page to go back to, but that argument doesn't actually exist. This commit adds the code so that goBack works as documented.

goBack( to:object )

Forces jQTouch to go back to a certain page in the history. You can
pass a specific page ID, a number of pages to go back, or nothing to
go back one page. If the specified page can not be found, jQTouch will
go back one page by default. Example:

jQT.goBack(2); // Goes back 2 pages in history
jQT.goBack('#home'); // Goes to #home as long as it's in history
jQT.goBack(); // Goes back 1 page in history

David Hull Change goBack() to take an optional count or hash argument.
goBack( to:object )

Forces jQTouch to go back to a certain page in the history. You can
pass a specific page ID, a number of pages to go back, or nothing to
go back one page. If the specified page can not be found, jQTouch will
go back one page by default. Example:

  jQT.goBack(2); // Goes back 2 pages in history
  jQT.goBack('#home'); // Goes to #home as long as it's in history
  jQT.goBack(); // Goes back 1 page in history
795c4a7
@thomasyip
Owner

Hi David, would you mind doing a simple CLA? You just need to do it one.
http://www.sencha.com/cla

Sencha requires anyone who contributes to fill the form. I did it myself.

@levic levic referenced this pull request from a commit in levic/jQTouch
Levi Cameron Add argument to .goBack()
Fixes issue #406
a6d537a
@levic levic referenced this pull request from a commit in levic/jQTouch
Levi Cameron Add argument to .goBack()
Fixes issue #406
4dad056
@thomasyip thomasyip closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 4, 2012
  1. Change goBack() to take an optional count or hash argument.

    David Hull authored
    goBack( to:object )
    
    Forces jQTouch to go back to a certain page in the history. You can
    pass a specific page ID, a number of pages to go back, or nothing to
    go back one page. If the specified page can not be found, jQTouch will
    go back one page by default. Example:
    
      jQT.goBack(2); // Goes back 2 pages in history
      jQT.goBack('#home'); // Goes to #home as long as it's in history
      jQT.goBack(); // Goes back 1 page in history
This page is out of date. Refresh to see the latest.
Showing with 40 additions and 6 deletions.
  1. +40 −6 src/jqtouch.js
View
46 src/jqtouch.js
@@ -122,9 +122,14 @@
}
}
- function doNavigation(fromPage, toPage, animation, goingBack) {
+ // popCount: if 0 (or undefined) this is a forward navigation,
+ // and toPage will be added to the history; if positive this
+ // is a back navigation, and popCount entries will be popped
+ // from the history.
+ function doNavigation(fromPage, toPage, animation, popCount) {
- goingBack = goingBack ? goingBack : false;
+ popCount = popCount || 0;
+ var goingBack = popCount > 0;
// Error check for target page
if (toPage === undefined || toPage.length === 0) {
@@ -226,7 +231,10 @@
// Housekeeping
$currentPage = toPage;
if (goingBack) {
- history.shift();
+ var i = popCount;
+ while (i-- > 0) {
+ history.shift();
+ }
} else {
addPageToHistory($currentPage, animation);
}
@@ -257,7 +265,7 @@
function getOrientation() {
return orientation;
}
- function goBack() {
+ function goBack(toPage) {
// Error checking
if (history.length < 1 ) {
@@ -270,9 +278,35 @@
}
var from = history[0],
- to = history[1];
+ popCount = 0;
+
+ if (toPage) {
+ if (typeof toPage === 'number') {
+ if (toPage > 0 && toPage < history.length) {
+ popCount = toPage;
+ toPage = history[popCount].page;
+ } else {
+ warn('toPage '+toPage+' out of history range.');
+ }
+ } else if (typeof toPage === 'string') {
+ find_history_entry: {
+ for (var i = 1; i < history.length; i++) {
+ if (history[i].hash === toPage) {
+ popCount = i;
+ toPage = history[i].page;
+ break find_history_entry;
+ }
+ }
+ warn('toPage '+toPage+' not found in history.');
+ toPage = $(toPage);
+ }
+ }
+ } else {
+ popCount = 1;
+ toPage = history[popCount].page;
+ }
- if (doNavigation(from.page, to.page, from.animation, true)) {
+ if (doNavigation(from.page, toPage, from.animation, popCount)) {
return publicObj;
} else {
warn('Could not go back.');
Something went wrong with that request. Please try again.