Skip to content

Instantly share code, notes, and snippets.

@shazron
Created May 23, 2013 22:03
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save shazron/5639820 to your computer and use it in GitHub Desktop.
Save shazron/5639820 to your computer and use it in GitHub Desktop.
RequireJS with Cordova example (app.js)
requirejs(['cordova.js'],
function () {
// start of require
// cordova is now available globally
var exec = cordova.require('cordova/exec');
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
// end of require
});
@Rastopyr
Copy link

Rastopyr commented Jan 3, 2014

Thanks, man!
I'm tinkered with this problem(requirejs and deviceready event) 3 hours! You has saved me from some time search problems!
Thank you again!

@navinSing
Copy link

can you pls share the other code snippets also ..
please please .. trying hard for last 8 hours now..

cordova version : 5.4 and requirejs

@romuloinnocencio
Copy link

I'm trying too... but not really easy.....

  1. I start a project with Cordova create blablabla;
  2. "script type="text/javascript" data-main="js/index.js" src="vendor/require/require.min.js"" in index.html;
  3. Uncaught ReferenceError: io is not defined(anonymous function) @ (index):161(anonymous function) @ (index):183
    require.min.js:1 Uncaught Error: Mismatched anonymous define() module: function (){var define,module,e
requirejs(['cordova.js'],function(){
    // cordova is now available globally
    var exec = cordova.require('cordova/exec');              
    var app = {
        // Application Constructor
        initialize: function() {
            this.bindEvents();
        },
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        },
        onDeviceReady: function() {
            app.receivedEvent('deviceready');
        },
    };             
    app.initialize();   
});

I can't understand how can i load RequireJS, with my CommonJS(configs) after device is ready =/
Some can help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment