Ad

Meteor - Not Updating In Real Time Anymore

- 1 answer

Just started using Meteor and am loving it thus far. I'm making a variation of the tutorial "todo" app, but with my own customization. More or less the changes I made so far have been:

  • Changed directory structure to client, server, public, lib/collections folders

  • Made the task collection through collection2 schema rather than dynamically creating with Tasks.insert(title = text,etc etc)

So to the question. These are my client/server files -

server.js

Meteor.publish("tasks", function () {
return Tasks.find({}, {sort: {checked: 1}});
});

client.js

// This code only runs on the client
Meteor.subscribe("tasks");

Template.task.events({
"click .toggledone": function () {
// Set the checked property to the opposite of its current value
Tasks.update(this._id, {
  $set: {checked: ! this.checked}
});
},
"click .delete": function () {
Tasks.remove(this._id);
}
});

And its being displayed in the standard html

  <ul>
  {{#each tasks}}
    {{> task}}
  {{/each}}
</ul>

So basically as it is now, my server is sorting the tasks on the basis of whether it is checked (completed) or not, ascending.

It works currently, when I click a task on the list, it turns red (my configuration) and registers as completed, but the list doesn't sort automatically like it did originally (before I made the directory changes).

For it to update, I need to refresh the page which is not what I want.

I'm new to this and i think it has something to do with my publish/subscribe but I can't seem to find anything to help me online.

Can anyone point me in the right direction for this?

Thanks so much.

Ad

Answer

I think you are confused between the publication/subscription named tasks and the collection tasks for the loop on the template. When you want to render the tasks on the template, it should be returned from a helper. Consider that publication/subscription making the correct part of the database available on the client and then, you retrieve the correct data from that part to display.

Normally, I make the naming convention for the publication like the SQL query so it is easier to interpret

Meteor.publish("select-from-tasks", function () {
    return Tasks.find({});
});

and then,

Meteor.subscribe("select-from-tasks");

You will need to have a helper to return the correct data, like I said above

Template.task.helpers({
    tasks: function() {return Tasks.find({}, {sort: {checked: 1}});}
});
Ad
source: stackoverflow.com
Ad