123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- Tasks = new Mongo.Collection("tasks");
- if (Meteor.isServer) {
- Meteor.publish('tasks', function () {
- var ret = Tasks.find({
- $or: [
- { private: { $ne: true }},
- { owner: this.userId }
- ]
- });
- return ret;
- })
- }
- if (Meteor.isClient) {
- // This code only runs on the client
- Meteor.subscribe('tasks');
- Template.body.helpers({
- tasks: function () {
- if (Session.get('hideCompleted')) {
- return Tasks.find({ checked : { $ne : true }}, { sort: { createdAt: -1 }});
- } else {
- return Tasks.find({}, {sort: {createdAt: -1}});
- }
- },
- hideCompleted: function () {
- var ret = Session.get("hideCompleted");
- return ret;
- },
- incompleteCount: function () {
- var ret = Tasks.find({ checked: { $ne: true }}).count();
- return ret;
- }
- });
- // Inside the if (Meteor.isClient) block, right after Template.body.helpers:
- Template.body.events({
- "submit .new-task": function (event) {
- // This function is called when the new task form is submitted
- var text = event.target.text.value;
- // Insert a task into the collection.
- Meteor.call('addTask', text);
- // Clear form
- event.target.text.value = "";
- // Prevent default form submit
- return false;
- },
- 'change .hide-completed input': function (e) {
- Session.set('hideCompleted', e.target.checked);
- }
- });
- Template.task.helpers({
- isOwner: function () {
- return this.owner === Meteor.userId();
- }
- });
- Template.task.events({
- 'click .delete': function (e) {
- Meteor.call('deleteTask', this._id);
- },
- 'click .toggle-checked' : function (e) {
- Meteor.call('setChecked', this._id, !this.checked);
- },
- 'click .toggle-private': function (e) {
- Meteor.call('setPrivate', this._id, ! this.private);
- }
- });
- Accounts.ui.config({
- passwordSignupFields: "USERNAME_ONLY"
- });
- }
- Meteor.methods({
- addTask: function (text) {
- if (!Meteor.userId()) {
- throw new Meteor.Error('not-authorized');
- }
- var task = {
- text: text,
- createdAt: new Date(),
- owner: Meteor.userId(),
- username: Meteor.user().username
- };
- Tasks.insert(task);
- },
- deleteTask: function (taskId) {
- // Even not logged-in ?
- Tasks.remove(taskId);
- },
- setChecked: function (taskId, setChecked) {
- Tasks.update(taskId, { $set : { checked: setChecked }});
- },
- setPrivate: function (taskId, setToPrivate) {
- var task = Tasks.findOne(taskId);
- // Make sure only the task owner can make the task private.
- if (task.owner !== Meteor.userId()) {
- throw new Meteor.Error('not-authorized');
- }
- Tasks.update(taskId, { $set: { private: setToPrivate }});
- }
- });
|