我开发有很多sequentail页面(例如:首页 - > 1 - >第2页 - >第3页)混合应用程序。所有这些页面都在标题数据添加回-BTN =设置为true,他们通过AJAX加载DATAS。问题是,当我按下后退按钮,我想清除页面的内容,因为当我在那个页面再去就会显示出$ P $,直到新的数据加载。我已经试过pvious数据
$('#page_one')。在('pagebeforechange',函数(){ //明确 //重载阿贾克斯}
但它的工作原理是,当页面被调用,而且当我在该网页上回返回从page_two。我想使用类似的onDestroy()在Android中。是否存在?
非常感谢你。
解决方案您需要利用两个事件
导航
来确定用户是否正在航行的回的使用JQM后退按钮数据相对=背
或浏览器的后退按钮。
VAR方向=;$(窗口)。在(导航功能(即数据){ 方向= data.state.direction ==背?真假;});
pagebeforechange
(JQM< = 1.3) pagecontainerbeforechange
(JQM> = 1.4)。此事件将返回你太导航后退按钮是否使用网页的对象 .toPage
。在他的舞台,可以更换的 toPage
内容。
的$(document)。在(pagecontainerbeforechange功能(即数据){ 如果(方向和放大器;&安培; $ .TYPE(data.toPage)==对象){ / *做你的魔法在这里* / $(UI内容,data.toPage).append(< P>返回按钮被击中< / P>中); / *不要忘记复位方向变更* / 方向= FALSE; }});
演示
I'm developing a hybrid application that have a lot of "sequentail" pages (Ex: Home -> Page 1 ->Page 2 -> Page 3). All this pages have set in the header data-add-back-btn= true and they load datas by AJAX. The problem is that when I push the back button I'd like to clear the content of the page because when I go in that page again it will show the previous data until the fresh data are loaded.. I've tried:
$('#page_one').on('pagebeforechange', function(){
//clear
//reload ajax
}
But it works yes when the page is called but also when I return back on that page from page_two. I'd like to use something like onDestroy() in Android. Does it exist ?
Thank You very much.
解决方案You need to utilize two events
navigate
to determine whether a user is navigating back using jQM back button data-rel="back"
or browser's back button.
var direction = "";
$(window).on("navigate", function (e, data) {
direction = data.state.direction == "back" ? true : false;
});
pagebeforechange
(jQM <= 1.3) pagecontainerbeforechange
(jQM >= 1.4). This event will return an object .toPage
of the page you are navigating too whether or not back button is used. At his stage, you can replace contents of toPage
.
$(document).on("pagecontainerbeforechange", function (e, data) {
if (direction && $.type(data.toPage) == "object") {
/* do your magic here */
$(".ui-content", data.toPage).append("<p>Back button was hit</p>");
/* don't forget to reset direction var */
direction = false;
}
});
Demo