/
app.js
115 lines (101 loc) · 2.67 KB
/
app.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Welcome to Pebblebase!
*
* This is where you write your Firebase powered Pebble app.
*/
var UI = require('ui');
var Vector2 = require('vector2');
require('./firebase');
/*
MAKE SURE TO REPLACE YOUR_FIREBASE WITH A LINK TO YOUR FIREBASE, OTHERWISE THIS WONT WORK
*/
var ref = new Firebase(YOUR_FIREBASE);
function showMenu() {
// We create a simple menu with a few options
var menu = new UI.Menu({
sections: [{
items: [{
title: 'View Data',
subtitle: '(in Firebase)'
},
{
title: 'Set a value',
subtitle: '(in Firebase)'
},
{
title: 'Push a value',
subtitle: '(in Firebase)'
},
{
title: 'Delete data',
subtitle: '(in Firebase)'
}]
}]
});
// When the user selects an option...
menu.on('select', function(e) {
// We use the title to perform operations
switch (e.item.title) {
case "View Data":
// Show the data view window
showData();
break;
case "Set a value":
// Set a location in Firebase
ref.child('set/value').set(true);
break;
case "Push a value":
// Push the current time into a firebase "array"
ref.child("push").push((new Date().toString()));
break;
case "Delete data":
// ... or remove all the data at the location
ref.remove();
break;
}
});
// Finally make sure the menu is set to show.
menu.show();
}
function showData() {
// Create a new Pebble window
var wind = new UI.Window();
// Then create a textfield which will be updated by Firebase.
var textfield = new UI.Text({
position: new Vector2(0, 50),
size: new Vector2(144, 30),
font: 'gothic-24-bold',
text: "Loading...",
textAlign: 'center'
});
// Add the textfield to our window object
wind.add(textfield);
// Create a new Firebase ref which points to the last child of /push
var recentPushRef = ref
.child('push')
.limitToLast(1);
// Pull up the most recent time pushed into our Firebase ref.
recentPushRef.on('child_added', function (datetime) {
var text = datetime.val();
textfield.text(text);
wind.show();
});
/*
We use a once event to determine if location exists
i.e. has any data been written
*/
recentPushRef.once('value', function (snapshot) {
if (!snapshot.exists())
textfield.text("No data, yet!");
wind.show();
});
/*
Make sure we're only getting updates from
Firebase when the card is displayed, so we .off when the window hides
*/
wind.on('hide', function () {
ref.child('push').off('child_added');
});
}
// Make sure we show our main menu
showMenu();