Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're checking for the wrong events, pageinit and pageshow are what you should be concerned about.</p> <p><strong>pageinit</strong> 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)</p> <p><strong>pageshow</strong> 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</p> <p>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 <br/> <br/></p> <p>Here's a good design for larger websites that we use in a production environment:</p> <ol> <li><p>bind a live event to all pages/dialogs pageinit and pageshow events in some include that is on every page:</p> <p><code>$(document).on('pageinit pageshow', 'div:jqmData(role="page"), div:jqmData(role="dialog")', function(event){</code></p></li> <li><p>I reference each page with a name: <code>&lt;div data-role="page" data-mypage="employeelist"&gt;</code> 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</p></li> <li><p>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 <code>&lt;script/&gt;</code> within the div[data-role="page"] - ignoring any JS in the <code>&lt;head/&gt;</code>, placing separate JS on each page is a mess and should be avoided in any large site I believe</p></li> <li><p>Try not to use blanket selectors in your jQuery, e.g. <code>$('div.myClass')</code> since this will search all of your DOM which may have more than one jQM page in it. Luckily in the live event handler for pageinit/pageshow mentioned above, <code>this</code> refers to the current page. So do all DOM searches within it, e.g. <code>$(this).find('div.myClass')</code> this ensures you are only grabbing elements within the current page. (of course this isn't a concern for ids). <em>Note in the pageshow event you can also use <code>$.mobile.activePage</code>, but this isn't available in the pageinit, so I don't use it for consistency</em></p></li> </ol> <p><br/> I eventually had too much code, so I built a handler object where each page's js is included in a separate js file and can register handlers with the live event</p> <p>The drawback is that all your js for your entire site is loaded on the first page the user reaches, but minified even a large site is smaller than jQuery or jQM so this shouldn't be a concern. <em>But if your site really is large I suppose you could look into RequireJS.</em></p> <p>An advantage is you are no longer loading all your JS for each page through AJAX each time the user navigates to a new page. <strong>If all your JS is available on entry, you can now put debugger statements and debug much more easily!</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload