How to disable the default JQM page in a SPA?

时间秒杀一切 提交于 2019-12-20 06:25:13

问题


I have a single page JQM application - all JQM "pages" are in one document:

    <div>
        @RenderPage("Views/login.cshtml")
        @RenderPage("Views/page1.cshtml")
        @RenderPage("Views/page2.cshtml")
    </div>

How can i disable displaying a JQM page by default, which currently happens to be the login.cshtml, so i can manually invoke the transition method to erase the login page from the history, after the user has logged in ? Eq this is what i want to invoke manually on initial document load:

$.mobile.changePage('#login-page', { reverse: false, changeHash: true });

Alternatively, is it possible to set the "reverse" attribute to false without a changePage call?


回答1:


If you want I can give you a client side solution.

Basically you an use my older example: http://jsfiddle.net/Gajotres/3PhKZ/ to prevent page transition if some condition is not fulfilled. And at that point you can forward that user at any other page with changePage function.

This solution will work in any case because it will trigger during every page transition and if next page is in your case login page it will look at conditions and do whatever you want. You can allow that transition, prevent it altogether or simply redirect to an another page:

$(document).on('pagebeforechange', function(e, data){  
    var to = data.toPage,
        from = data.options.fromPage;

    if (typeof to  === 'string') {
        var u = $.mobile.path.parseUrl(to);
        to = u.hash || '#' + u.pathname.substring(1);
        if (from) from = '#' + from.attr('id');

        if (from === '#index' && to === '#second') {
            alert('Can not transition from #index to #second!');
            e.preventDefault();
            e.stopPropagation();

            // remove active status on a button, if transition was triggered with a button
            $.mobile.activePage.find('.ui-btn-active').removeClass('ui-btn-active ui-focus ui-btn');;
        }  
    }
});


来源:https://stackoverflow.com/questions/16682517/how-to-disable-the-default-jqm-page-in-a-spa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!