Question

We have a PhoneGap mobile project which uses Dojo. In a given HTML page, e.g. index.html, we would have a JavaScript tag similar to this:

<script>
    require(["path/to/myApp", "dojo/domReady!"], function(MyApp) {
        new MyApp().startup();
    });
</script>

path/to/myApp.js would then be a class which performs the necessary functions, e.g.:

define(["dojo/_base/declare",
    "dojox/mobile/deviceTheme",
    "dojox/mobile/compat"],
    function(declare) {
        return declare(null, {
            startup: function() {
                // create and display views etc.
            }
        });
    });

What we are trying to do is implement an auto-update facility along the following lines:

  • At startup, use AJAX to check with the server if the client app version is current.
  • If the client is out of date, send new JavaScript (and/or HTML, CSS) to the client as needed.
  • The client stores the JavaScript in local storage on the device.

Given this scheme, at startup the client would read the JavaScript from local storage and execute that code, replacing path/to/myApp.js. What I am struggling with is tying this into Dojo's "require" AMD functionality. I think I would still need to use the Dojo loader in order to resolve dependencies etc.

From what I can see, the Dojo loader uses "require" to locate a DOM node to which it then adds a "script" tag, and finally it sets the "src" attribute of the script tag to "path/to/myApp.js" (in this example). I need a way to change this behavior to instead run the updated JavaScript which is now fetched from local storage on the device. This will enable us to auto-update our app from the server without requiring repeated re-deployments to the app store for small things like bug fixes.

Was it helpful?

Solution

There is an AMD loader that does what you are looking for:

It also contains a timestamp functionality where you can send a JSON object with the current file timestamps to the server. You would then need to write a server component that returns an array of files to update.

The "Getting started" section of the wiki contains a Dojo example so it should be possible to use with Dojo. However, you will be replacing the Dojo loader with another AMD compliant loader, which might cause problems. My guess would be that this will not be a problem.

This is not the standard way to update Apps, why not just use the normal phone app update route? It will mean that an Internet connection is needed for first use of each component. Components are loaded only when they are required into the code. You might have a component hidden deep in the application that is not loaded and then suddenly the user needs an Internet connection.

However, I assume you have considered all these things and if your App requires an always on Internet connection anyway, this will not be an issue.

You are also introducing an extra security concern for your app users. Should your server be hacked then code could be sent to all your users and executed on their phones. The standard App Store Update functionality adds another layer of security (albeit small) for your users.

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