Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQueryMobile: pagecontainershow on a particular page not working

JQueryMobile 1.4 has deprecated the pageshow event and instead recommends using pagecontainershow; however, while I'm able to get the pagecontainershow event at a document level, I can't bind a function to a particular page.

<div id="page1" data-role="page">
...
<script>
  $( "#page1" ).on( "pagecontainershow", function( event, ui ) {
    console.log("page1 pagecontainershow");
  } );
</script>
</div>

Demonstration: http://jsbin.com/IFolanOW/22/edit?html,console,output

I also considered using the alternative form of the jQuery "on" function where we use a selector, but that would need to be a parent of the page div, and that might include other pages, so that doesn't work.

As a workaround, I've done this, but it is very inefficient:

function registerOnPageShow(pageId, func) {
    var strippedPageId = pageId.replace("#", "");
    var e = "pagecontainershow." + strippedPageId;
    // TODO why isn't it working to use $(pageId) instead of $(document)?
    $( document ).off(e).on(e, null, {page: strippedPageId, f: func}, function(e, ui) {
      if ($(":mobile-pagecontainer").pagecontainer("getActivePage")[0].id == e.data.page) {
        e.data.f(e, ui);
      }
    });
}
like image 711
Infofinity Avatar asked Dec 29 '13 19:12

Infofinity


3 Answers

You can get the page ID like this.

$(document).on('pagecontainershow', function(e, ui) {
    var pageId = $('body').pagecontainer('getActivePage').prop('id');
});

There is currently no way to have a show/hide event on a specific page.

like image 154
aggsol Avatar answered Oct 12 '22 16:10

aggsol


Here is what I'm using (jqmobile >1.4):

$(document).on("pagecontainershow", function () {
    var activePage = $.mobile.pageContainer.pagecontainer("getActivePage");

    var activePageId = activePage[0].id;
    switch (activePageId) {
        case 'loginPage':
            ...
            break;
        case 'homePage':
            ...
            break;
        case 'groupPage':
            ...
            break;
        default:
    }
});
like image 38
Assaf S. Avatar answered Oct 12 '22 16:10

Assaf S.


$(document).on("pagecontainershow", function(event, ui) {
   var pageId = $('body').pagecontainer('getActivePage').prop('id'),
       showFunc = pageId+'_show';

   if (typeof MobileSite[showFunc] == 'function') {
      MobileSite[showFunc]();
   }
});

MobileSite is contained in an external .js file with all the show() functions.

like image 24
Ben Avatar answered Oct 12 '22 16:10

Ben