How to use custom build from dojo under file:/// protocol (Cordova/Phonegap)

429 views Asked by At

I develop a web app using the dojo framework. This app must be loaded by a browser using the file:/// protocol (It will become a Cordova/Phonegap application for Android/iPad). The index.html file looks like:

Head section:

<script type="text/javascript">
    var dojoConfig =  {
        parseOnLoad: true
    }
</script>

<script type="text/javascript" src=""dojo.js"/>

<script type="text/javascript">
  dojo.require("dojox.mobile.parser");
  dojo.require("dojox.mobile");
</script>

Body section:

<script type="text/javascript">
    dojo.require("dojox.mobile.parser");
    dojo.require("dojox.mobile");
</script>

It was taken from IBM lesson (Mobile application development, Part 1: PhoneGap and Dojo Mobile on Android)

So there are two problems:

1) dojo tries to load modules but failed to do it using the file:/// protocol. The HTTP protocol is requiered.

2) In the index.html I use a custom build of dojo made by the web builder. This build consists of a single file which contains modules I need. However these modules must be required which makes the framework try to load them from unexisting files though they has been already loaded in the big custom dojo.js

May be someone knows how to solve these two problems.

2

There are 2 answers

0
frank On

Which browser are you testing your code on? Chrome does not allow multiple domains within a single Html file, due to security concerns. Try your code on the firefox browser it should prompt a warning and run the code. Also make sure the index.html file are in the parent directory of the dojo source (dojo library). i.e

your directory structure should be as shown below
|--[dojo_source/dojo_library]
|
|-index.html.

also run your code with the file protocol in the browser file://

Hope this helps.

Another alternative is to install a web server. You can try the "mongoose" web server which is very light just 136KB in size. Which you can then setup to run your dojo code. I have been using the web server and it has been great for unit testing my code.

0
Flowkap On

You simply can't use dojo via file protocol as the AMD engine is lazy loading the dependencies via XHR requests which won't work that way.

Refer to: http://www.sitepen.com/blog/2014/07/30/dojo-faq-why-do-i-need-to-use-an-http-server-with-dojo/

Think about simply using a node js webserver for development and embedding a small web service in the phone application as well.

You could still try to access modules via the obsolete but still available interface dojo.someModule as the modules are defined in the global namespace due to compatibility issues. However i'm not sure if this is working well.

You could also try to set async to false in the dojo config.