TODO: wait for all subs to be ready, need a better loading strategy - might be a blog post and something I can use for the talk
Currently we are automatically publishing data from the server. That's fine for prototyping purposes, but for a real app, we want control what is published and what the client subscribes to.
meteor remove autopublish
You should now see that there are no notes being displayed.
Our notes are still in the database. However, we now need to explicitly publish and subscribe to our notes.
/imports/collections/server/publications.js
import { Meteor } from 'meteor/meteor'
import { Notes } from '../notes'
const
notesListFields = {
title: 1,
updatedAt: 1
}
Meteor.publish('notes.list', function() {
return Notes.find({}, { fields: notesListFields})
})
TODO: change meteor methods naming convention to be consistent. TODO: discuss - why are we not using arrow function?
Why the server directory? Why only publish certain fields?
/imports/startup/server/index.js
...
import '/imports/collections/server/publications'
/imports/components/containers/homepage_container.js
export default createContainer(() => {
const
sub = Meteor.subscribe('notes.list')
,
notes = sub.ready()? Note.find({}, { sort: { updatedAt: -1}}).fetch() : []
,
...
}, App)
TODO: explain sub.ready() and use of empty array
Your notes should now re-appear in your browser.