Skip to content
Permalink
Browse files

Added edit posts form

chapter8-1
  • Loading branch information
tmeasday committed Oct 19, 2015
1 parent 3f25a2a commit 85f91b09d69ef88e24d532f031b8217a7e7bc264
@@ -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>
@@ -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');
}
}
});
@@ -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>
@@ -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;
@@ -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() {

3 comments on commit 85f91b0

@lwwlhj

This comment has been minimized.

Copy link

@lwwlhj lwwlhj replied May 17, 2016

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

@lwwlhj

This comment has been minimized.

Copy link

@lwwlhj lwwlhj replied May 18, 2016

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

@lwwlhj

This comment has been minimized.

Copy link

@lwwlhj lwwlhj replied May 18, 2016

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.
You can’t perform that action at this time.