Alexandru
Alexandru

Reputation: 981

Socket.io variable not defined in Phonegap mobile application

I'm trying to develop a mobile application using Phonegap with RequireJs, Backbone, jQuery and I'm running into a problem each time I try to include the requirejs script tag:

<script data-main="js/app" src="node_modules/requirejs/require.js</script>

After I include this, I get the following error:

ReferenceError: io is not defined.

I'm not using socket.io but I think that Phonegap is using it in order to refresh the page in the browser.

This is my index.html file:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />

    <!-- This is a wide open CSP declaration. To lock this down for production, see below. -->
    <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" />

    <title>Hello World</title>
</head>
<body>

    something nice

    <!-- Content -->

    <script type="text/javascript" src="cordova.js"></script>
    <script data-main="js/app" src="node_modules/requirejs/require.js"></script>
</body>
</html>

This is my js/app.js file:

requirejs.config({

    baseUrl: 'js',

    shim: {
        'socket.io': {
            exports: 'io'
        },
        'underscore': {
            exports: '_'
        },
        'backbone': {
            deps: [
                'underscore',
                'jquery'
            ],
            exports: 'Backbone'
        }
    },

    paths: {

        jquery: 'jquery.min',
        underscore: 'lodash.min',
        backbone: 'backbone',
        socketio: '../socket.io/socket.io'

        // package: 'node_modules'
    }

    // map: {
    //
    //     '*': {
    //         'jquery': 'private/jquery'
    //     },
    //
    //     'private/jquery': {
    //         'jquery': 'jquery'
    //     }
    // }
});

I'm using Phonegap version 6.3.4.

Could you please tell me, what should i do in order to get rid of the error?

Thank you!

Upvotes: 1

Views: 309

Answers (1)

Alexandru
Alexandru

Reputation: 981

I've managed to solve it by reading these articles:

Here's my fix:

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

        // Fixes "Uncaught ReferenceError: io is not defined".
        // We need to load RequireJs after socket.io has been loaded.

        function injectRequireJs() {
            var h = document.getElementsByTagName('body')[0];
            var s = document.createElement('script');
            s.type = 'text/javascript';
            s.src = 'node_modules/requirejs/require.js';
            s.setAttribute('data-main', 'js/bootstrap');
            h.appendChild(s);
        }

        setTimeout(function(){
            injectRequireJs();
        }, 1);

    </script>

Upvotes: 1

Related Questions