-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo-meteor.js
70 lines (58 loc) · 1.52 KB
/
mongo-meteor.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
Values = new Mongo.Collection("values");
data = []
if (Meteor.isClient) {
Meteor.subscribe('values');
// This code only runs on the client
Template.body.helpers({
values: function () {
return Values.find({}, { sort: { createdAt: -1 } });
}
});
Template.body.events({
"submit .new-value": function (event) {
var v = event.target.value.value;
Values.insert({
percentage: parseInt(v),
createdAt: new Date() // current time
});
// Clear form
event.target.text.value = "";
// Prevent default form submit
return false;
}
});
Template.chart.onRendered(function() {
Meteor.subscribe("values", function(){
var init = true;
Values.find({}).observeChanges({
added: function (id, fields) {
if (!init) {
data.push({ x: fields.createdAt.getTime(), y: fields.percentage });
graph.render();
}
}
});
init = false;
values = Values.find({});
values.forEach(function(v) {
data.push({ x: v.createdAt.getTime(), y: v.percentage });
});
graph = new Rickshaw.Graph({
element: document.querySelector("#chart"),
width: 285,
height: 180,
series: [{
color: 'steelblue',
data: data
}]
});
var xAxis = new Rickshaw.Graph.Axis.Time({graph: graph});
graph.render();
});
});
}
if (Meteor.isServer) {
Meteor.publish('values', function() {
return Values.find({});
});
}