Question

I'm using worklight 6.0.

The deviceready event is not firing. I'm testing this in the MBS, in iOS and Android both in Firefox and Chrome.

Here is the code I have used in initoptions.js file:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
     alert("device Ready")
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

alert is never getting fired.

I think worklight 6.0 using cordova 2.6 version, Do I need to change the cordova.js of any other version?

Was it helpful?

Solution

This check is not needed in a Worklight-based application.

As you correctly mentioned, Cordova is bundled with Worklight; Once your app successfully launches, the deviceReady has already fired (internally). Otherwise, your app would not work to begin with.

App launch > Cordova initialization > Worklight framework initialization > App is "ready"


So what you need to do is to simply place your implementation code in common\yourApp.js (or wherever you want and then call it), w/out the deviceReady check.

For example, you'll note that if doing the following, it will work even w/out the check for devicReady first.

function wlCommonInit() {
    alert (device.cordova);
}

If to use the fileSystem example, which is what you are trying to do... I have done the following and it is working OK for me. I simply followed the LocalFileSystem example from the Cordova File API page.

  1. New project and application
  2. Added the Android environment

    The File API is meant for mobile environments and not for previewing in your PC browser.
    This is the reason for the error you see: LocalFileSystem is not defined. It has got nothing to do with deviceReady.

  3. In common\testapp.js I have added the following

  4. Ran the app in Android Emulator
  5. In the app, I got the success alert()

    function wlCommonInit(){
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
    }
    
    function onFileSystemSuccess(fileSystem) {
       alert("*** Success *** File system name is: " + fileSystem.name);
    }
    
    function fail(evt) {
        alert("*** Fail *** Error code is: " + evt.target.error.code);
    }
    

To do the same when previewing the Android environment via Worklight Console's MBS, you need to make sure you have Java installed and that it is active in your browser (to view Java applets). You should then use the File menu on the left pane.

See these questions:

If you are on Mac, you need to use Java 6.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top