Skip to content

Instantly share code, notes, and snippets.

@zeroasterisk
Last active September 22, 2018 17:15

Revisions

  1. zeroasterisk revised this gist Apr 17, 2013. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion client_application.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,14 @@ Session.set('CordovaLoaded', false);
    Meteor.startup(function() {
    console.log('cordova loading');
    // delay loading of cordova until after DOM is ready
    $.getScript('/cordova-2.6.0.js', function() {
    // 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');
    // --------------------------
  2. zeroasterisk created this gist Apr 17, 2013.
    59 changes: 59 additions & 0 deletions client_application.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    // 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
    $.getScript('/cordova-2.6.0.js', 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;
    };