Shen liang
Shen liang

Reputation: 1435

When using with requireJS, $ionicPlatform.ready (ionic v1.0.0-beta.11) not firing

As mentioned in title, the function won't be triggered when register the callback function using $ionicPlatform.ready or ionic.Platform.ready

Upvotes: 0

Views: 859

Answers (1)

Shen liang
Shen liang

Reputation: 1435

The reason is because ionic.Platform is using 'load' event to register the 'deviceready' event. Since requireJS is used, the 'load' event will be triggered before ionic get chance to hook the 'load' event. Then ionic miss the 'deviceready' event. The solution is pretty simple, just trigger another 'load' event to let ionic calls itself initialization callback function or register your own 'deviceready' handler directly on the document.

 window.addEventListener("load", onWindowLoad, false);

Using the following code to test it out

 <body onload="console.log('body onload triggered')">
 <script src="bower_components/requirejs/require.js"></script>
 <script src="scripts/requirejsconfig.js"></script>
 <script src="cordova.js"></script>
 </body>

In the 'requirejsconfig.js'

  //pre-load the non AMD libraries for the r.js optimization
  require([   'domReady!',
        'angular',
        'ionic',
        'ngIonic',
        ...
    ],
    function (document, ng, ionic)
    {
        'use strict';

        console.log('call ionic Platform ready');
        ionic.Platform.ready(function ()
                            {
                                'use strict';
                                ng.bootstrap(document, ['app']);
                            });
    });

The output in chrome will be

  1. body onload triggered
  2. hook load event in ionic with this window
    this is output by adding 'console.log('hook load event in ionic with this window');' into ionic.js just before 'window.addEventListener("load", onWindowLoad, false);function onPlatformReady() {...'
  3. call ionic Platform ready

The output in Xcode5.1 will be (the HTML log info is gone, because of the cordova reset which is before 3th)

  1. Resetting plugins due to page load.
  2. Finished load of: file:///Users...
  3. hook load event in ionic with this window
  4. call ionic Platform ready

Upvotes: 1

Related Questions