-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-todos.js
105 lines (89 loc) · 2.94 KB
/
simple-todos.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
tasks = new SQL.Collection('tasks','postgres://postgres:1234@localhost/postgres');
username = new SQL.Collection('username','postgres://postgres:1234@localhost/postgres');
if (Meteor.isClient) {
Session.set('user', 'all')
var taskTable = {
id: ['$number'],
text: ['$string', '$notnull'],
checked: ['$bool'],
usernameid: ['$number']
};
tasks.createTable(taskTable);
var usersTable = {
id: ['$number'],
name: ['$string', '$notnull']
};
username.createTable(usersTable);
Template.body.helpers({
usernames: function () {
return username.select().fetch();
},
tasks: function () {
if (Session.get('user') === 'all'){
return tasks.select('tasks.id', 'tasks.text', 'tasks.checked', 'tasks.createdat', 'username.name')
.join(['OUTER JOIN'], ['usernameid'], [['username', ['id']]])
.fetch();
}
return tasks.select('tasks.id', 'tasks.text', 'tasks.checked', 'tasks.createdat', 'username.name')
.join(['OUTER JOIN'], ['usernameid'], [['username', ['id']]])
.where("name = ?", Session.get('user'))
.fetch();
}
});
Template.body.events({
"submit .new-task": function (event) {
if (event.target.category.value){
var text = event.target.text.value;
var user = username.select('id')
.where("name = ?", event.target.category.value)
.fetch()[0].id;
tasks.insert({
text: text,
checked:false,
usernameid: user
}).save();
event.target.text.value = "";
} else{
alert("please add a user first");
}
return false;
},
"submit .new-user": function (event) {
var text = event.target.text.value;
username.insert({
name:text
}).save();
event.target.text.value = "";
return false;
},
"click .toggle-checked": function () {
tasks.update({id: this.id, "checked": !this.checked})
.where("id = ?", this.id)
.save();
},
"click .delete": function () {
tasks.remove()
.where("id = ?", this.id)
.save();
},
"change .catselect": function(event){
Session.set('user',event.target.value);
}
});
}
if (Meteor.isServer) {
tasks.createTable({text: ['$string'], checked: ["$bool", {$default: false}]}).save();
username.createTable({name: ['$string', '$unique']}).save();
tasks.createRelationship('username', '$onetomany').save();
tasks.publish('tasks', function(){
return tasks.select('tasks.id as id', 'tasks.text', 'tasks.checked', 'tasks.createdat', 'username.id as usernameid', 'username.name')
.join(['INNER JOIN'], ["usernameid"], [["username", 'id']])
.order('createdat DESC')
.limit(100);
});
username.publish('username', function(){
return username.select('id', 'name')
.order('createdat DESC')
.limit(100);
});
}