working on posts routing

This commit is contained in:
Sacha Greif 2012-09-05 11:55:06 +09:00
parent cdf9fed589
commit 6026259033
8 changed files with 106 additions and 48 deletions

View file

@ -29,14 +29,26 @@ if (Meteor.is_client) {
'signin':'signin',
'signup':'signup',
'submit':'submit',
'posts/:id':'post'
'posts/:id':'post',
'comments/:id':'comment'
},
top: function() { console.log("top"); this.goto('top'); },
test: function() {console.log("test"); this.goto('test'); },
signup: function() {console.log("signup"); this.goto('signup'); },
signin: function() {console.log("signin"); this.goto('signin'); },
submit: function() {console.log("submit"); this.goto('submit'); },
post: function(id) {console.log("post, id="+id); Session.set('selected_post_id', id); this.goto('post'); },
post: function(id) {
console.log("post, id="+id);
Session.set('selected_post', Posts.findOne({_id:id}));
Session.set('selected_post_id', id);
this.goto('post');
},
comment: function(id) {
console.log("comment, id="+id);
Session.set('selected_comment', Comments.findOne({_id:id}));
Session.set('selected_comment_id', id);
this.goto('single_comment');
},
});
var Router = new SimpleRouter();

View file

@ -14,10 +14,10 @@
<div class="comment-meta">
<a class="comment-username">{{submitter}}</a>
<span class="comment-time">{{ago}}</span>
<a class="comment-permalink icon-link">link</a>
<a href="/comments/{{_id}}" class="comment-permalink icon-link goto-comment">link</a>
</div>
<div class="comment-text">{{body}}</div>
<a href="#" class="comment-reply">Reply</a>
<a href="/comments/{{_id}}" class="comment-reply goto-comment">Reply</a>
</div>
{{#unless repress_recursion}}
<ul class="comment-children">

View file

@ -1,8 +1,11 @@
Template.comment.events = {
'click .comment-reply': function(event){
'click .goto-comment': function(event){
event.preventDefault();
var href=event.target.href.replace(/^(?:\/\/|[^\/]+)*\//, "");
Session.set('selected_comment', this);
Session.set('state', 'reply');
// Session.set('state', 'reply');
Router.navigate(href, {trigger: true});
}
};
@ -12,7 +15,7 @@ Template.comment.ago = function(){
};
Template.comment.child_comments = function(){
var post = Session.get('selected_post');
var comments = Comments.find({ post: post._id, parent: this._id });
var post_id = Session.get('selected_post_id');
var comments = Comments.find({ post: post_id, parent: this._id });
return comments;
};

View file

@ -65,4 +65,3 @@ Template.item.domain = function(){
a.href = this.url;
return a.hostname;
};

View file

@ -2,7 +2,7 @@ Template.post.events = {
'click input[type=submit]': function(e){
e.preventDefault();
var post = Posts.findOne({_id: Session.get('selected_post_id')});
var post = Session.get('selected_post');
var $comment = $('#comment');
Meteor.call('comment', post, null, $comment.val());
$comment.val('');
@ -18,17 +18,18 @@ Template.post.show_comment_form = function(){
};
Template.post.post = function(){
var post = Posts.findOne({_id: Session.get('selected_post_id')});
var post = Posts.findOne(Session.get('selected_post_id'));
return post;
};
Template.post.has_comments = function(){
console.log(Session.get('selected_post_id'));
var post_id = Session.get('selected_post_id');
return Comments.find({post: post_id, parent: null}).count() > 0;
var post = Posts.findOne(Session.get('selected_post_id'));
if(post){
return Comments.find({post: post._id, parent: null}).count() > 0;
}
};
Template.post.child_comments = function(){
var post_id = Session.get('selected_post_id');
return Comments.find({post: post_id, parent: null});
var post = Posts.findOne(Session.get('selected_post_id'));
return Comments.find({post: post._id, parent: null});
};

View file

@ -1,32 +0,0 @@
Template.selected_comment.events = {
'click input[type=submit]': function(evt){
evt.preventDefault();
var post = Session.get('selected_post');
var parentComment = Session.get('selected_comment');
var $comment = $('#comment');
Meteor.call('comment', post, parentComment, $comment.val());
Session.set('selected_comment', null);
Session.set('state', 'view_post');
}
};
Template.selected_comment.show = function(){
return Session.equals('state', 'reply');
};
Template.selected_comment.show_comment_form = function(){
return Meteor.user() !== null;
};
Template.selected_comment.post = function(){
var post = Session.get('selected_post');
return post;
};
Template.selected_comment.comment = function(){
var comment = Session.get('selected_comment');
comment.repress_recursion = true;
return comment;
};

View file

@ -0,0 +1,29 @@
<template name="single_comment">
<div class="post grid">
{{#if postLoaded}}
{{#with post}}
{{> item}}
{{#if body}}
<div class="post-message">{{body}}</div>
{{/if}}
{{/with}}
{{/if}}
{{#with comment}}
<ul class="selected-comment">
{{> comment}}
</ul>
{{/with}}
{{#if show_comment_form}}
<div class="comment-new">
<form>
<div class="comment-field">
<textarea id="comment" rows="3" autofocus="autofocus"></textarea>
</div>
<div class="comment-submit">
<input type="submit" class="button" value="Add Reply" />
</div>
</form>
</div>
{{/if}}
</div>
</template>

View file

@ -0,0 +1,46 @@
Template.single_comment.events = {
'click input[type=submit]': function(e){
e.preventDefault();
var post = Session.get('selected_post');
var post_id=Session.get('selected_post_id');
var parentComment = Session.get('selected_comment');
var $comment = $('#comment');
Meteor.call('comment', post, parentComment, $comment.val());
Session.set('selected_comment', null);
// Session.set('state', 'view_post');
Router.navigate('posts/'+post_id, {trigger:true});
}
};
Template.single_comment.show = function(){
return Session.equals('state', 'reply');
};
Template.single_comment.show_comment_form = function(){
return Meteor.user() !== null;
};
Template.single_comment.postLoaded = function(){
var selected_comment = Comments.findOne(Session.get('selected_comment_id'));
if(selected_comment){
return true;
}else{
return false;
}
}
Template.single_comment.post = function(){
var selected_comment = Comments.findOne(Session.get('selected_comment_id'));
if(selected_comment){
var post = selected_comment.post;
return Posts.findOne(post);
}
};
Template.single_comment.comment = function(){
var comment = Comments.findOne({_id:Session.get('selected_comment_id')});
Template.single_comment.repress_recursion = true;
return comment;
};