1

Using the latest version of both Phonegap and JQM, how do I got about setting up the two events, deviceready and mobileinit?

(I have looked through the answers on here but they are outdated and give little info)

I am currently using the onload function, but there is severe lag - I can click a button that should fire a notification and it is delayed by several seconds.

Should I be using the jQuery $(document).ready()?

What is the correct order for mobileinit and deviceready?

Where should my own code go?

current code below:

<script type="text/javascript" src="phonegap.js"></script>
    <script type="text/javascript" src="js/jquery-2.1.0.min.js"></script>
    <script type="text/javascript" src="js/jquery.mobile-1.4.2.min.js"></script>

    <script type="text/javascript">
    $(document).bind("mobileinit", function() {
        $.mobile.allowCrossDomainPages = true;
        $.mobile.buttonMarkup.hoverDelay = 0;
        $.mobile.defaultDialogTransition = 'none';
        $.mobile.defaultPageTransition = 'none';
    });    

    function onLoad() {
      document.addEventListener("deviceready", onDeviceReady, false);
    }

    function onDeviceReady() {
      $(document).on('click', '.add-alarm', function() {
        var now = new Date().getTime(),
        _60_seconds_from_now = new Date(now + 5*1000);

        window.plugin.notification.local.add({
          title: 'title herre',
          message: 'my message',
          date: _60_seconds_from_now,
          sound: 'TYPE_ALARM'
        });
      });
    }
    </script>

onLoad() fired by

<body onload="onLoad()">
Omar
  • 32,302
  • 9
  • 69
  • 112
Mr Pablo
  • 4,109
  • 8
  • 51
  • 104
  • Wrap `click` listener` in `pagecreate` instead of `onDeviceReady()` and call `onLoad()`. `$(document).on("pagecreate", "#pageID", function () { click event here });`. – Omar Mar 13 '14 at 16:32
  • btw, `mobileinit` should go after jQuery.js and before jQM.js – Omar Mar 13 '14 at 16:37
  • What should I do with the deviceready event listener? I just tried wrapping the click event in pagecreate, it didn't work, in fact it made things worse. the alert didn't pop up. – Mr Pablo Mar 13 '14 at 16:41
  • Hmmm..good question. I dont know Phonegap, maybe you should keep it. But for jQM use _page events_ e.g. `pagecreate` to attach listeners/bind events. – Omar Mar 13 '14 at 16:42
  • The answer described [here](http://stackoverflow.com/questions/10945643/correct-way-of-using-jquery-mobile-phonegap-together/12821151#12821151) is personally what I use. And it's not outdated. – Drew B. Mar 13 '14 at 21:35
  • If I used that solution, where do I put the configuration settings for JQM (the ones inside mobileinit typically) – Mr Pablo Mar 13 '14 at 23:12

0 Answers0