Sunday, May 18, 2014

Update notes on Discover Meteor book

Meteor is undergoing rapid changes, which makes certain parts of the Discover Meteor book outdated. These are my notes for each chapter as I am going through the book.

Collections

Defining the insert record code in fixture.js does not seed the database when it is empty and I'm not sure how to fix that yet.


Routing

Although the routing package is no longer supported it is still available, but it didn't work out the box for me and I didn't feel like spending time to figure it out, so I switched to Iron Router instead. The routing syntax is different.

  • post_page.html > remove {{#with currentPost}}, use Iron Router data attribute to retrieve the post record

    this.route('postPage', {
       path: '/posts/:_id',
       data: function() { return Posts.findOne(this.params._id); }
      });


Latency Compensation

For the latency compensation demonstration to work and to take advantage of latency compensation in general the following changes are needed

  • post method needs to be defined on both the server and client. 
  • Future ret() function is now return()


----- Client Code ------

Meteor.methods({
post: function(postAttributes) {
var postWithSameLink = Posts.findOne({url: postAttributes.url});

// ensure the post has a title
if (!postAttributes.title)
throw new Meteor.Error(422, "Please fill in a headline");

// check that there are no prevous posts with the same link
if (postAttributes.url && postWithSameLink) {
throw new Meteor.Error(302, 
"This link has already been posted",
postWithSameLink._id);
}

console.log("isSimulation", this.isSimulation);
// pick out the whitelisted keys
var post = _.extend(_.pick(postAttributes, 'url'), {
title: postAttributes.title + (this.isSimulation ? '(client)' : '(server)')
});

Posts.insert(post);
}
});

----- Server Code ------

Meteor.publish('posts', function() {
  return Posts.find();
});

Meteor.methods({
post: function(postAttributes) {
var user = Meteor.user(),
postWithSameLink = Posts.findOne({url: postAttributes.url});

// ensure user is logged in
if (!user)
throw new Meteor.Error(401, "You need to login to post new stories");

// ensure the post has a title
if (!postAttributes.title)
throw new Meteor.Error(422, "Please fill in a headline");

// check that there are no prevous posts with the same link
if (postAttributes.url && postWithSameLink) {
throw new Meteor.Error(302,
"This link has already been posted",
postWithSameLink._id);
}

// pick out the whitelisted keys
var post = _.extend(_.pick(postAttributes, 'url'), {
title: postAttributes.title + (this.isSimulation ? '(client)' : '(server)'),
userId: user._id,
author: user.username,
submitted: new Date().getTime()
});

if(!this.isSimulation) {
var Future = Npm.require('fibers/future');
var future = new Future();
Meteor.setTimeout(function() {
future.return();
}, 5 * 1000);
future.wait();
}

var postId = Posts.insert(post);
return postId;
}

});

No comments:

Post a Comment