Skip to content

Instantly share code, notes, and snippets.

@zeroasterisk
Last active September 22, 2018 17:15
Show Gist options
  • Save zeroasterisk/5405344 to your computer and use it in GitHub Desktop.
Save zeroasterisk/5405344 to your computer and use it in GitHub Desktop.
MeteorJS code for attaching PhoneGap events, and doing things inside MeteorJS. note: as of now, I'm loading cordova via an AJAX request because it was complaining on direct compile (js bugs in console)... I may keep working on getting it to load with the site/app directly, but the rest of the attached events would remain the same, just without t…
// initialize as soon as the DOM is ready
Session.set('CordovaLoaded', false);
Meteor.startup(function() {
console.log('cordova loading');
// delay loading of cordova until after DOM is ready
// determine WHICH cordova to load
var cordovajspath = '/cordova-2.6.0.js';
if (navigator.userAgent.match(/(iPad|iPhone|iOS)/) != null) {
cordovajspath = '/cordova-2.6.0-ios.js';
} else if (navigator.userAgent.match(/(Android)/) != null) {
cordovajspath = '/cordova-2.6.0-android.js';
}
$.getScript(cordovajspath, function() {
Session.set('CordovaLoaded', true);
console.log('cordova loaded');
// --------------------------
// SETUP for Devices
// --------------------------
if (! (document.deviceEventsSetup)) {
// http://docs.phonegap.com/en/2.6.0/cordova_events_events.md.html#Events
document.deviceEventsSetup = true;
// when an app 'resumes'
// This is an event that fires when a Cordova application is retrieved from the background.
document.addEventListener("resume", function() {
// http://docs.meteor.com/#meteor_reconnect
// Force an immediate reconnection attempt if the client is not connected
// to the server.
// This method does nothing if the client is already connected.
Meteor.reconnect();
Meteor.resume();
});
// when an app goes into the background
document.addEventListener("Pause", function() {
Cookie.set('LastPage', Meteor.Router.page());
});
// when an app drops 'offline'
document.addEventListener("offline", function() {
if (Meteor.Router.page() != 'offline' && Meteor.Router.page() != 'loading') {
Cookie.set('LastPage', Meteor.Router.page());
Meteor.Router.to('/offline');
}
});
// when an app comes 'online'
document.addEventListener("online", function() {
Meteor.resume();
});
}
});
});
// resume functionality, common (used in offline.js as well)
Meteor.resume = function() {
if (Meteor.status().status != 'connected') {
return false;
}
if (Meteor.Router.page() != 'offline' && Meteor.Router.page() != 'loading') {
return true;
}
var LastPage = Cookie.get('LastPage');
if (_.isString(LastPage) && LastPage.length && LastPage != 'loading') {
console.log('resumed to: (LastPage)', '/' + LastPage);
Meteor.Router.to('/' + LastPage);
return true;
}
Meteor.Router.to('/');
return true;
};
@raix
Copy link

raix commented Apr 17, 2013

+i on match? as I recall some androids are lowercase - but nice!

@zol
Copy link

zol commented Apr 18, 2013

That's helpful, thanks!

@zol
Copy link

zol commented Apr 18, 2013

Actually, what's '/cordova-2.6.0.js'? I'm looking for a cordova stub for when the app is running as a webapp, have you seen something like it?

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