jQuery Mobile pageinit/pagecreate not firing

后端 未结 7 1922
栀梦
栀梦 2020-12-05 05:24

I have 5 pages - for ease lets say:

  • one.html
  • two.html
  • three.html
  • four.html
  • five.html

When I load each indivi

7条回答
  •  无人及你
    2020-12-05 06:07

    You're checking for the wrong events, pageinit and pageshow are what you should be concerned about.

    pageinit fires everytime a page is loaded for the first time, jQM caches pages in the DOM/memory so when you navigate back from two.html to one.html, pageinit won't fire (it's already initialized)

    pageshow fires everytime a page is shown, this is what you need to be looking for when you navigate back from two.html to one.html

    Together you can pull off a lot of clean code, use pageinit for initializing, configuration etc and update your DOM to the initial state. If you have dynamic data on the page that may change between views, handle it in pageshow

    Here's a good design for larger websites that we use in a production environment:

    1. bind a live event to all pages/dialogs pageinit and pageshow events in some include that is on every page:

      $(document).on('pageinit pageshow', 'div:jqmData(role="page"), div:jqmData(role="dialog")', function(event){

    2. I reference each page with a name:

      In this live event you can basically have a switch statement for each page "name", and then check event.type for pageinit/pageshow or both and put your code there, then every time a page is created/shown this event will be fired, it knows what page triggered it and then calls the corresponding code

    3. Now no matter what entry point a user lands on your site, all the handlers for all the pages are loaded. As you may already know, when you navigate to a page, it only pulls in

提交回复
热议问题