Skip to content

Commit

Permalink
Editing errors chapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
SachaG committed Oct 15, 2014
1 parent 8b92fae commit 1523c4a
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 45 deletions.
32 changes: 28 additions & 4 deletions 07-creating-posts.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,18 @@ Template.postSubmit.events({
<%= caption "client/views/posts/post_submit.js" %>
<%= highlight "10~16" %>

The `Meteor.call` function calls a Method named by its first argument. You can provide arguments to the call (in this case, the `post` object we constructed from the form), and finally attach a callback, which will execute when the server-side Method is done. Here we simply alert the user if there's a problem, or redirect the user to the freshly created post's discussion page if not.
The `Meteor.call` function calls a Method named by its first argument. You can provide arguments to the call (in this case, the `post` object we constructed from the form), and finally attach a callback, which will execute when the server-side Method is done.

We then define the Method in our `collections/posts.js` file. We'll remove the `allow()` block from `posts.js` since Meteor Methods bypass them anyway. Remember that Methods are executed on the server, so Meteor assumes they can be trusted.
Meteor method callbacks always have two arguments, `error` and `result`. If for whatever reason the `error` argument exists, we'll alert the user (using `return` to abort the callback). If everything's working as it should, we'll redirect the user to the freshly created post's discussion page.

### Security Check

We'll also take advantage of this opportunity to add a little more security to our method by using the [`audit-argument-checks`](http://docs.meteor.com/#auditargumentchecks) package.
We'll take advantage of this opportunity to add a little more security to our method by using the [`audit-argument-checks`](http://docs.meteor.com/#auditargumentchecks) package.

This package lets you check any JavaScript object against a predefined pattern. In our case, we'll use it to check that the user calling the method is properly logged in (by making sure that `Meteor.userId()` is a `String`), and that the `postAttributes` object being passed as argument to the method contains `title` and `url` strings.

So let's define the `postInsert` method in our `collections/posts.js` file. We'll remove the `allow()` block from `posts.js` since Meteor Methods bypass them anyway.

We'll then `extend` the `postAttributes` object with three more properties: the user's `_id` and `username`, as well as the post's `submitted` timestamp, before inserting the whole thing in our database and returning the resulting `_id` to the client (in other words, the original caller of this method) in a JavaScript object.

~~~js
Expand Down Expand Up @@ -386,6 +388,16 @@ Meteor.methods({

Note that the `_.extend()` method is part of the [Underscore](http://underscorejs.org) library, and simply lets you “extend” one object with the properties of another.

<% note do %>

### Bye Bye Allow/Deny

Meteor Methods are executed on the server, so Meteor assumes they can be trusted. As such, Meteor methods bypass any allow/deny callbacks.

If you want to run some code before every `insert`, `update`, or `remove` *even on the server*, we suggest checking out the [collection-hooks](https://github.com/matb33/meteor-collection-hooks) package.

<% end %>

### Preventing Duplicates

We'll make one more check before wrapping up our method. If a post with the same URL has already been created previously, we won't add the link a second time but instead redirect the user to this existing post.
Expand Down Expand Up @@ -478,4 +490,16 @@ Template.postsList.helpers({

It took a bit of work, but we finally have a user interface to let users securely enter content in our app!

But any app that lets users create content also needs to give them a way to edit or delete it. That's what the Editing Posts chapter will be all about.
But any app that lets users create content also needs to give them a way to edit or delete it. That's what the Editing Posts chapter will be all about.

<% note do %>

### Methods vs Allow/Deny

As you've seen here, Meteor apps can use two different patterns to insert and modify data: you can either call `insert`, `update`, and `remove` from client in conjunction with allow/deny rules, or else call your own custom Meteor method.

So which approach is best? The short answer is that while the allow/deny pattern is certainly elegant, it's also very easy to get wrong. As such, we tend to recommend using methods instead.

We've written more about this vast topic in [this blog post](https://www.discovermeteor.com/blog/meteor-methods-client-side-operations/).

<% end %>
Loading

8 comments on commit 1523c4a

@Slava
Copy link
Member

@Slava Slava commented on 1523c4a Oct 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great job, but yeah. when are we going to translate it :D

@SachaG
Copy link
Member Author

@SachaG SachaG commented on 1523c4a Oct 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah there won't be that much time before 1.0… Maybe I can help coordinate the translation effort a bit better using a chatroom or Telescope board?

@Slava
Copy link
Member

@Slava Slava commented on 1523c4a Oct 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SachaG dunno, I would feel bad reaching out to all the amazing people who contributed to the Russian translation asking them to translate this change

@SachaG
Copy link
Member Author

@SachaG SachaG commented on 1523c4a Oct 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh? What do you mean?

@Viktorminator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SachaG , @Slava , have you ever met smth like https://crowdin.com/ ? This would solve translation issues. Or it's better to ask (search for) professional translation tools (for group translation) if they exist.

@arunoda
Copy link

@arunoda arunoda commented on 1523c4a Oct 15, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SachaG
Copy link
Member Author

@SachaG SachaG commented on 1523c4a Oct 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Viktorminator well, the main issue is that people will have to retranslate the text since it's changed, I don't really see a way around it…

That being said I don't think Crowdin would work for a book. You need the context of the book, you can't just translate isolated strings like in a user interface. Also how would you even separate the book into individual sentences?

We just had a thread about this very topic over at the Japanese repo and @nwilkes at least seemed to agree that tools like Crowdin aren't really appropriate for books.

@rgoomar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree with Sacha that it would be too difficult to just isolate strings to send to something like crowdin. Although it is a good tool, it may not be the right tool for the book.

Please sign in to comment.