Skip to content

Commit

Permalink
Added edit posts form
Browse files Browse the repository at this point in the history
chapter8-1
  • Loading branch information
tmeasday committed Oct 19, 2015
1 parent 3f25a2a commit 85f91b0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
19 changes: 19 additions & 0 deletions client/templates/posts/post_edit.html
@@ -0,0 +1,19 @@
<template name="postEdit">
<form class="main form page">
<div class="form-group">
<label class="control-label" for="url">URL</label>
<div class="controls">
<input name="url" id="url" type="text" value="{{url}}" placeholder="Your URL" class="form-control"/>
</div>
</div>
<div class="form-group">
<label class="control-label" for="title">Title</label>
<div class="controls">
<input name="title" id="title" type="text" value="{{title}}" placeholder="Name your post" class="form-control"/>
</div>
</div>
<input type="submit" value="Submit" class="btn btn-primary submit"/>
<hr/>
<a class="btn btn-danger delete" href="#">Delete post</a>
</form>
</template>
31 changes: 31 additions & 0 deletions client/templates/posts/post_edit.js
@@ -0,0 +1,31 @@
Template.postEdit.events({
'submit form': function(e) {
e.preventDefault();

var currentPostId = this._id;

var postProperties = {
url: $(e.target).find('[name=url]').val(),
title: $(e.target).find('[name=title]').val()
}

Posts.update(currentPostId, {$set: postProperties}, function(error) {
if (error) {
// display the error to the user
alert(error.reason);
} else {
Router.go('postPage', {_id: currentPostId});
}
});
},

'click .delete': function(e) {
e.preventDefault();

if (confirm("Delete this post?")) {
var currentPostId = this._id;
Posts.remove(currentPostId);
Router.go('postsList');
}
}
});
4 changes: 4 additions & 0 deletions client/templates/posts/post_item.html
Expand Up @@ -2,6 +2,10 @@
<div class="post">
<div class="post-content">
<h3><a href="{{url}}">{{title}}</a><span>{{domain}}</span></h3>
<p>
submitted by {{author}}
{{#if ownPost}}<a href="{{pathFor 'postEdit'}}">Edit</a>{{/if}}
</p>
</div>
<a href="{{pathFor 'postPage'}}" class="discuss btn btn-default">Discuss</a>
</div>
Expand Down
3 changes: 3 additions & 0 deletions client/templates/posts/post_item.js
@@ -1,4 +1,7 @@
Template.postItem.helpers({
ownPost: function() {
return this.userId == Meteor.userId();
},
domain: function() {
var a = document.createElement('a');
a.href = this.url;
Expand Down
5 changes: 5 additions & 0 deletions lib/router.js
Expand Up @@ -12,6 +12,11 @@ Router.route('/posts/:_id', {
data: function() { return Posts.findOne(this.params._id); }
});

Router.route('/posts/:_id/edit', {
name: 'postEdit',
data: function() { return Posts.findOne(this.params._id); }
});

Router.route('/submit', {name: 'postSubmit'});

var requireLogin = function() {
Expand Down

3 comments on commit 85f91b0

@lwwlhj
Copy link

@lwwlhj lwwlhj commented on 85f91b0 May 17, 2016

Choose a reason for hiding this comment

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

‘Delete’ works but “Modification of the userId and URL" doesn't work. that is, posts can be deleted but can't be updated.

@lwwlhj
Copy link

@lwwlhj lwwlhj commented on 85f91b0 May 18, 2016

Choose a reason for hiding this comment

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

More specifically,
var postProperties = { url: $(e.target).find('[name=url]').val(), title: $(e.target).find('[name=title]').val() }
seems doesn't work

@lwwlhj
Copy link

@lwwlhj lwwlhj commented on 85f91b0 May 18, 2016

Choose a reason for hiding this comment

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

Sorry, I realized I made a mistake, something wrong with my post_edit.html. So, the codes are good, sorry again

Please sign in to comment.