简体   繁体   中英

JQuery Mobile Override Change Page

What I trying to accomplish is not record certain pages in the history, so the back button will not take you back to the previous page.

For example, if the page is the login page or register page, do not record it, else record the page :

function changePage(page)
if(page == 'login' || page == 'register') {

$.mobile.changePage( "#" + page , {
    transition: "pop",
    reverse: false,
    changeHash: false
});

} else {
$.mobile.changePage( "#" + page, {
    transition: "pop",
    reverse: true,
    changeHash: true
});
}

}//end change page

But I want to ovveride the mobile.changePage function in a global way so it checks each the function is called. Can anyone give me any ideas how to do this?

You can replace all calls to changePage() in your code with a custom function myChangePage() and extend jQuery Mobile as shown below:

$(document).bind('mobileinit', function(){

  $.mobile.myChangePage = function(page, options){

    if ($.inArray(page.substr(1), ['login', 'register']))
    {
      $.extend(options, {
        reverse: false,
        changeHash: false
      });
    }

    $.mobile.changePage(page, options);

  };
});

Example usage:

$.mobile.myChangePage('#login');

I would save the function of change page to a variable then if in some instances you don't need to override the function you can proxy right back to the function:

var cp = $.mobile.changePage;
$.mobile.changePage = function(to, options) {
    if(youdontneedtoverride) {
        cp(to, options);
    } else {
        ....
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM