Permalink
Browse files

Use a method to submit the post.

chapter7-6
  • Loading branch information...
tmeasday committed Oct 15, 2014
1 parent bf44a99 commit 2357173519c6b252d0c428d5692f260c49fff9b1
Showing with 30 additions and 6 deletions.
  1. +2 −0 .meteor/packages
  2. +1 −0 .meteor/versions
  3. +7 −2 client/templates/posts/post_submit.js
  4. +20 −4 lib/collections/posts.js
View
@@ -22,3 +22,5 @@ iron:router@1.0.0-rc.1
sacha:spin
accounts-password
ian:accounts-ui-bootstrap-3
check
audit-argument-checks
View
@@ -1,6 +1,7 @@
accounts-base@1.2.1
accounts-password@1.1.3
anti:i18n@0.4.3
audit-argument-checks@1.0.4
autoupdate@1.2.3
babel-compiler@5.8.24_1
babel-runtime@0.1.4
@@ -7,7 +7,12 @@ Template.postSubmit.events({
title: $(e.target).find('[name=title]').val()
};
post._id = Posts.insert(post);
Router.go('postPage', post);
Meteor.call('postInsert', post, function(error, result) {
// display the error to the user and abort
if (error)
return alert(error.reason);
Router.go('postPage', {_id: result._id});
});
}
});
View
@@ -1,8 +1,24 @@
Posts = new Mongo.Collection('posts');
Posts.allow({
insert: function(userId, doc) {
// only allow posting if you are logged in
return !! userId;
Meteor.methods({
postInsert: function(postAttributes) {
check(Meteor.userId(), String);
check(postAttributes, {
title: String,
url: String
});
var user = Meteor.user();
var post = _.extend(postAttributes, {
userId: user._id,
author: user.username,
submitted: new Date()
});
var postId = Posts.insert(post);
return {
_id: postId
};
}
});

4 comments on commit 2357173

@zhenhappy

This comment has been minimized.

Show comment
Hide comment
@zhenhappy

zhenhappy Oct 22, 2015

我执行了meteor add audit-argument-checks后感觉不是很正确,没有出现check这个包,只能手动添加这行

我执行了meteor add audit-argument-checks后感觉不是很正确,没有出现check这个包,只能手动添加这行

@zebra-li

This comment has been minimized.

Show comment
Hide comment
@zebra-li

zebra-li Oct 26, 2015

这里少了 check 包的添加,需要add check包才行

这里少了 check 包的添加,需要add check包才行

@qingchen1990sc

This comment has been minimized.

Show comment
Hide comment
@qingchen1990sc

qingchen1990sc Nov 18, 2015

确认需要meteor add check

确认需要meteor add check

@lwwlhj

This comment has been minimized.

Show comment
Hide comment
@lwwlhj

lwwlhj Apr 27, 2016

u need add check man

u need add check man

Please sign in to comment.