Hey - so imagine the app is a todo list with two columns.
incomplete column which shows incomplete tasks
completed column which shows completed tasks
Thing is, when I mark a task as completed, I don’t want it to jump from one column to another. In other words, I want the just-completed task to remain in the incomplete column but be marked as completed. Still, when the client refreshes the browser, I want the task to be in the completed column.
I found a way to make this work using the private Tasks._collection collection on the client, which only updates client data without syncing with the server. Therefore, when the client refreshes, the change to the client collection is not persisted.
I wanted to know if there was a cleaner way to do this. Here is the code.
// template.js (client)
Template.todo.helpers({
'uncompletedTasks': function() {
// should return incomplete tasks and tasks that have been completed
// during this browser session
return Tasks.find({$or: [{completed: false}, {justCompleted: true}]});
},
'completedTasks': function() {
return Tasks.find({completed: true, justCompleted: null});
}
});
Template.todo.events({
'click .done-button': function() {
Tasks._collection.update(this._id, {$set: {justCompleted: true}}); // here is what makes it work!
Meteor.call('completeTask', this._id);
}
});
// methods.js (both)
Meteor.methods({
'completeTask': function(taskId) {
Tasks.(taskId, {$set: {completed: true}});
}
});
Thanks, but I’m not exactly sure how this would help? I still need sync with server to mark tasks as completed There must be an easier way with an array tracking just-completed ids though.
EDIT: here is the code with an array as a ReactiveVar. Works but a bit more code though.
you could use codition in todo.js … you try code …this example
Template.todo.onCreated(function() {
if (Meteor.user()) {
this.subscribe(‘users’, Meteor.user().username)
}
});