Reputation: 4568
I have an issue with moving backwards through the history stack in my jquery mobile app.
Essentially, I have three pages:
The record pages are created dynamically based on a table and a record_id, appended to the document body each time a new record is selected.
var page_id = table + record_id;
var pg_html = newPageHTML(page_id );
$('body').append(pg_html);
$.mobile.changePage($("#" + page_id));
after advancing to a record and pressing the 'back' button (data-rel="back"), expected behavior would be to go back to the previous page, be it another record or the results list, but I am sent all the way back to search form. This occurs when i use data-dom-cache="true" and when I do not.
Any explanation as to why this would be? Thanks for the help.
Upvotes: 3
Views: 2510
Reputation: 4568
Alright, well, here is how I fixed it. Though I won't chose this as the answer because it is so far from how I had wanted it to work and seems highly convoluted.
The issues:
Now the html pages are stored and have all the info needed to redraw themselves on clicking back.
code for #2 (not mine, great thanks to whoever wrote it):
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
code for #3:
// change page
if($('.ui-page-active').attr('id') == "record-a"){
$.mobile.changePage("p-record-b.html?table=" + content_type + "&id=" + record_id);
}else if($('.ui-page-active').attr('id') == "record-b"){
$.mobile.changePage("p-record-a.html?table=" + content_type + "&id=" + record_id);
}else{
$.mobile.changePage("p-record-a.html?table=" + content_type + "&id=" + record_id);
}
Upvotes: 2
Reputation: 31732
Get the ID of the previous page in DOM then move to it.
$('.selector').on('click', function() {
// get the ID of the previous page
var previous = '#' + $.mobile.activePage.prev('div[data-role="page"]')[0].id;
// move to previous page with reverse effect
$.mobile.changePage(previous, {
transition: 'slide',
reverse: true
});
});
Upvotes: 2