-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.initialization.js
82 lines (73 loc) · 2.87 KB
/
app.initialization.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//-------------------------------------------------------
// Cordova App Object Literal
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);
// 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();
});
},
// 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);
}
};
//-------------------------------------------------------
// METEOR
// 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;
};