Switching from manually generating urls to using IronRouter functions.

Using {{pathFor}}, path(), and url() where possible. Passing in path
to Meteor.absoluteUrl() where the IronRouter functions didn't make sense.
Also deleting some random unused code.
This commit is contained in:
Anthony Mayer 2014-12-03 00:06:00 -08:00
parent 93e201de5f
commit 23b72b9cb8
23 changed files with 105 additions and 104 deletions

View file

@ -2,5 +2,5 @@
<title>Loading...</title> <title>Loading...</title>
<meta name="viewport" content="initial-scale=1.0"> <meta name="viewport" content="initial-scale=1.0">
<link rel="shortcut icon" href="/img/favicon.ico"/> <link rel="shortcut icon" href="/img/favicon.ico"/>
<link id="rss-link" rel="alternate" type="application/rss+xml" title="New Posts" href="/feed.xml"/> <link id="rss-link" rel="alternate" type="application/rss+xml" title="New Posts" href="{{pathFor route='feed'}}"/>
</head> </head>

View file

@ -29,7 +29,7 @@ Template[getTemplate('comment_edit')].events({
}); });
trackEvent("edit comment", {'postId': comment.postId, 'commentId': comment._id}); trackEvent("edit comment", {'postId': comment.postId, 'commentId': comment._id});
Router.go("/posts/"+comment.postId+"/comment/"+comment._id); Router.go('post_page_comment', {_id: comment.postId, commentId: comment._id});
}, },
'click .delete-link': function(e){ 'click .delete-link': function(e){
var comment = this; var comment = this;
@ -38,9 +38,8 @@ Template[getTemplate('comment_edit')].events({
if(confirm(i18n.t("are_you_sure"))){ if(confirm(i18n.t("are_you_sure"))){
Meteor.call('removeComment', comment._id); Meteor.call('removeComment', comment._id);
Router.go("/posts/"+comment.postId); Router.go('post_page', {_id: comment.postId});
throwError("Your comment has been deleted."); throwError("Your comment has been deleted.");
// Router.go("/comments/deleted");
} }
} }
}); });

View file

@ -28,7 +28,10 @@ Template[getTemplate('comment_form')].events({
throwError(error.reason); throwError(error.reason);
}else{ }else{
trackEvent("newComment", newComment); trackEvent("newComment", newComment);
Router.go('/posts/'+parentComment.postId+'/comment/'+newComment._id); Router.go('post_page_comment', {
_id: parentComment.postId,
commentId: newComment._id
});
} }
}); });
}else{ }else{

View file

@ -21,9 +21,9 @@
<a class="comment-username" href="{{profileUrl}}">{{authorName}}</a> <a class="comment-username" href="{{profileUrl}}">{{authorName}}</a>
<span class="comment-time">{{timeAgo ago}},</span> <span class="comment-time">{{timeAgo ago}},</span>
<span class="points">{{upvotes}}</span> <span class="unit">points </span> <span class="points">{{upvotes}}</span> <span class="unit">points </span>
<a href="/comments/{{_id}}" class="comment-permalink icon-link goto-comment">{{_ "link"}}</a> <a href="{{pathFor route='comment_reply' _id=_id}}" class="comment-permalink icon-link goto-comment">{{_ "link"}}</a>
{{#if can_edit}} {{#if can_edit}}
| <a class="edit-link" href="/comments/{{_id}}/edit">{{_ "edit"}}</a> | <a class="edit-link" href="{{pathFor route='comment_edit' _id=_id}}">{{_ "edit"}}</a>
{{/if}} {{/if}}
{{#if isAdmin}} {{#if isAdmin}}
| <span>{{full_date}}</span> | <span>{{full_date}}</span>
@ -31,7 +31,7 @@
</div> </div>
<div class="comment-text markdown">{{{htmlBody}}}</div> <div class="comment-text markdown">{{{htmlBody}}}</div>
{{#if getSetting "nestedComments" true}} {{#if getSetting "nestedComments" true}}
<a href="/comments/{{_id}}" class="comment-reply goto-comment">{{_ "reply"}}</a> <a href="{{pathFor route='comment_reply' _id=_id}}" class="comment-reply goto-comment">{{_ "reply"}}</a>
{{/if}} {{/if}}
</div> </div>
</div> </div>

View file

@ -43,30 +43,9 @@ Template[getTemplate('nav')].helpers({
} }
}); });
Template[getTemplate('nav')].rendered = function(){
if(!Meteor.loggingIn() && !Meteor.user()){
$('.login-link-text').text("Sign Up/Sign In");
}
};
Template[getTemplate('nav')].events({ Template[getTemplate('nav')].events({
'click #logout': function(e){
e.preventDefault();
Meteor.logout();
},
'click .mobile-menu-button': function(e){ 'click .mobile-menu-button': function(e){
e.preventDefault(); e.preventDefault();
$('body').toggleClass('mobile-nav-open'); $('body').toggleClass('mobile-nav-open');
},
'click .login-header': function(e){
e.preventDefault();
Router.go('/account');
},
'click #login-name-link': function(){
if(Meteor.user() && !$('account-link').exists()){
var $loginButtonsLogout = $('#login-buttons-logout');
$loginButtonsLogout.before('<a href="/users/'+Meteor.user().slug+'" class="account-link button">View Profile</a>');
$loginButtonsLogout.before('<a href="/account" class="account-link button">Edit Account</a>');
}
} }
}); });

View file

@ -11,7 +11,7 @@
</div> </div>
</div> </div>
{{else}} {{else}}
<a class="account-link sign-in" href="/sign-in">{{_ "sign_in"}}</a> <a class="account-link sign-in" href="{{pathFor route='atSignIn'}}">{{_ "sign_in"}}</a>
<a class="account-link sign-up" href="/sign-up">{{_ "sign_up"}}</a> <a class="account-link sign-up" href="{{pathFor route='atSignUp'}}">{{_ "sign_up"}}</a>
{{/if}} {{/if}}
</template> </template>

View file

@ -2,7 +2,7 @@
<div class="post-meta-item"> <div class="post-meta-item">
<span class="points">{{baseScore}} <span class="unit">{{pointsUnitDisplayText}} </span></span>by <a class="post-author" href="{{profileUrl}}">{{authorName}}</a> <span class="post-time">{{timeAgo ago}}</span> <span class="points">{{baseScore}} <span class="unit">{{pointsUnitDisplayText}} </span></span>by <a class="post-author" href="{{profileUrl}}">{{authorName}}</a> <span class="post-time">{{timeAgo ago}}</span>
{{#if can_edit}} {{#if can_edit}}
| <a href="/posts/{{_id}}/edit" class="edit-link goto-edit">{{_ "edit"}}</a> | <a href="{{pathFor route='post_edit' _id=_id}}" class="edit-link goto-edit">{{_ "edit"}}</a>
{{/if}} {{/if}}
</div> </div>
</template> </template>

View file

@ -26,7 +26,7 @@ AutoForm.hooks({
}else{ }else{
// note: find a way to do this in onSuccess instead? // note: find a way to do this in onSuccess instead?
trackEvent("edit post", {'postId': post._id}); trackEvent("edit post", {'postId': post._id});
Router.go('/posts/'+post._id); Router.go('post_page', {_id: post._id});
submit.done(); submit.done();
} }
}); });

View file

@ -28,9 +28,10 @@ AutoForm.hooks({
}else{ }else{
// note: find a way to do this in onSuccess instead? // note: find a way to do this in onSuccess instead?
trackEvent("new post", {'postId': post._id}); trackEvent("new post", {'postId': post._id});
if(post.status === STATUS_PENDING) if (post.status === STATUS_PENDING) {
throwError('Thanks, your post is awaiting approval.'); throwError('Thanks, your post is awaiting approval.');
Router.go('/posts/'+post._id); }
Router.go('post_page', {_id: post._id});
submit.done(); submit.done();
} }
}); });

View file

@ -15,7 +15,7 @@ Template[getTemplate('user_edit')].helpers({
return getGitHubName(this) || ""; return getGitHubName(this) || "";
}, },
profileUrl: function(){ profileUrl: function(){
return Meteor.absoluteUrl()+"users/"+this.slug; return getProfileUrlBySlugOrId(this.slug);
}, },
hasNotificationsUsers : function(){ hasNotificationsUsers : function(){
return getUserSetting('notifications.users', '', this) ? 'checked' : ''; return getUserSetting('notifications.users', '', this) ? 'checked' : '';

View file

@ -65,7 +65,7 @@
</thead> </thead>
{{#each posts}} {{#each posts}}
<tr> <tr>
<td><a href="/posts/{{_id}}/">{{title}}</a></td> <td><a href="{{pathFor route='post_page' _id=_id}}">{{title}}</a></td>
<td>{{formatDate createdAt "MM/DD/YYYY, HH:mm"}}</td> <td>{{formatDate createdAt "MM/DD/YYYY, HH:mm"}}</td>
</tr> </tr>
{{/each}} {{/each}}
@ -90,7 +90,7 @@
</thead> </thead>
{{#each upvotedPosts}} {{#each upvotedPosts}}
<tr> <tr>
<td><a href="/posts/{{_id}}/">{{title}}</a></td> <td><a href="{{pathFor route='post_page' _id=_id}}">{{title}}</a></td>
<td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td> <td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td>
</tr> </tr>
{{/each}} {{/each}}
@ -115,7 +115,7 @@
</thead> </thead>
{{#each downvoted}} {{#each downvoted}}
<tr> <tr>
<td><a href="/posts/{{_id}}/">{{title}}</a></td> <td><a href="{{pathFor route='post_page' _id=_id}}">{{title}}</a></td>
<td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td> <td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td>
</tr> </tr>
{{/each}} {{/each}}
@ -141,7 +141,7 @@
</thead> </thead>
{{#each comments}} {{#each comments}}
<tr> <tr>
<td><a href="/posts/{{postId}}/">{{postTitle}}</a></td> <td><a href="{{pathFor route='post_page' _id=postId}}/">{{postTitle}}</a></td>
<td>{{{htmlBody}}}</td> <td>{{{htmlBody}}}</td>
<td>{{formatDate createdAt "MM/DD/YYYY, HH:mm"}}</td> <td>{{formatDate createdAt "MM/DD/YYYY, HH:mm"}}</td>
</tr> </tr>

View file

@ -229,7 +229,7 @@ getPostProperties = function (post) {
var p = { var p = {
postAuthorName : getDisplayName(postAuthor), postAuthorName : getDisplayName(postAuthor),
postTitle : cleanUp(post.title), postTitle : cleanUp(post.title),
profileUrl: getProfileUrlById(post.userId), profileUrl: getProfileUrlBySlugOrId(post.userId),
postUrl: getPostPageUrl(post), postUrl: getPostPageUrl(post),
thumbnailUrl: post.thumbnailUrl, thumbnailUrl: post.thumbnailUrl,
linkUrl: !!post.url ? getOutgoingUrl(post.url) : getPostPageUrl(post._id) linkUrl: !!post.url ? getOutgoingUrl(post.url) : getPostPageUrl(post._id)

View file

@ -24,7 +24,11 @@ scrollPageTo = function(selector){
$('body').scrollTop($(selector).offset().top); $('body').scrollTop($(selector).offset().top);
}; };
getDigestURL = function(moment){ getDigestURL = function(moment){
return '/digest/'+moment.year()+'/'+(moment.month()+1)+'/'+moment.date(); return Router.routes['posts_digest'].path({
year: moment.year(),
month: moment.month() + 1,
day: moment.date()
});
}; };
getDateRange= function(pageNumber){ getDateRange= function(pageNumber){
var now = moment(new Date()); var now = moment(new Date());
@ -50,24 +54,27 @@ goTo = function(url){
}; };
getSignupUrl = function(){ getSignupUrl = function(){
return Meteor.absoluteUrl()+'sign-up'; return Router.routes['atSignUp'].url();
}; };
getSigninUrl = function(){ getSigninUrl = function(){
return Meteor.absoluteUrl()+'sign-in'; return Router.routes['atSignIn'].url();
}; };
getPostUrl = function(id){ getPostUrl = function(id){
return Meteor.absoluteUrl()+'posts/'+id; return Router.routes['post_page'].url({_id: id});
}; };
getPostEditUrl = function(id){ getPostEditUrl = function(id){
return Meteor.absoluteUrl()+'posts/'+id+'/edit'; return Router.routes['post_edit'].url({_id: id});
}; };
getCommentUrl = function(id){ getCommentUrl = function(id){
return Meteor.absoluteUrl()+'comments/'+id; return Router.routes['comment_reply'].url({_id: id});
}; };
getPostCommentUrl = function(postId, commentId){ getPostCommentUrl = function(postId, commentId){
// get link to a comment on a post page // get link to a comment on a post page
return Meteor.absoluteUrl()+'posts/'+postId+'/comment/'+commentId; return Router.routes['post_page_comment'].url({
_id: postId,
commentId: commentId
});
}; };
slugify = function(text) { slugify = function(text) {
if(text){ if(text){
@ -78,7 +85,7 @@ slugify = function(text) {
return text; return text;
}; };
getShortUrl = function(post){ getShortUrl = function(post){
return post.shortUrl ? post.shortUrl : post.url; return post.shortUrl || post.url;
}; };
getDomain = function(url){ getDomain = function(url){
urlObject = Npm.require('url'); urlObject = Npm.require('url');
@ -88,7 +95,7 @@ invitesEnabled = function () {
return getSetting("requireViewInvite") || getSetting("requirePostInvite"); return getSetting("requireViewInvite") || getSetting("requirePostInvite");
}; };
getOutgoingUrl = function(url){ getOutgoingUrl = function(url){
return getSiteUrl() + 'out?url=' + encodeURIComponent(url); return Router.routes['out'].url({}, {query: {url: url}});
}; };
// ---------------------------------- String Helper Functions ----------------------------------- // // ---------------------------------- String Helper Functions ----------------------------------- //
cleanUp = function(s){ cleanUp = function(s){

View file

@ -141,7 +141,7 @@ Meteor.startup( function (){
Session.set('categorySlug', null); Session.set('categorySlug', null);
// if we're not on the search page itself, clear search query and field // if we're not on the search page itself, clear search query and field
if(getCurrentRoute().indexOf('search') == -1){ if(Router.current().route.getName() !== 'search'){
Session.set('searchQuery', ''); Session.set('searchQuery', '');
$('.search-field').val('').blur(); $('.search-field').val('').blur();
} }

View file

@ -42,13 +42,10 @@ getDisplayNameById = function(userId){
return getDisplayName(Meteor.users.findOne(userId)); return getDisplayName(Meteor.users.findOne(userId));
}; };
getProfileUrl = function(user) { getProfileUrl = function(user) {
return Meteor.absoluteUrl()+'users/' + slugify(getUserName(user)); return getProfileUrlBySlugOrId(user.slug);
}; };
getProfileUrlById = function(id){ getProfileUrlBySlugOrId = function(slugOrId) {
return Meteor.absoluteUrl()+'users/'+ id; return Router.routes['user_profile'].url({_idOrSlug: slugOrId});
};
getProfileUrlBySlug = function(slug) {
return Meteor.absoluteUrl()+'users/' + slug;
}; };
hasPassword = function(user) { hasPassword = function(user) {
return !!user.services.password; return !!user.services.password;

View file

@ -27,7 +27,7 @@ var getCommenterProfileUrl = function (comment) {
if(user) { if(user) {
return getProfileUrl(user); return getProfileUrl(user);
} else { } else {
return getProfileUrlById(comment.userId) return getProfileUrlBySlugOrId(comment.userId)
} }
} }

View file

@ -1,5 +1,5 @@
getUnsubscribeLink = function(user){ getUnsubscribeLink = function(user){
return Meteor.absoluteUrl()+'unsubscribe/'+user.email_hash; return Router.routes['unsubscribe'].url({hash: user.email_hash});
}; };
// given a notification, return the correct subject and html to send an email // given a notification, return the correct subject and html to send an email
@ -26,7 +26,7 @@ buildEmailNotification = function (notification) {
var emailProperties = _.extend(notification.data, { var emailProperties = _.extend(notification.data, {
body: marked(comment.body), body: marked(comment.body),
profileUrl: getProfileUrlById(comment.userId), profileUrl: getProfileUrlBySlugOrId(comment.userId),
postCommentUrl: getPostCommentUrl(post._id, comment._id), postCommentUrl: getPostCommentUrl(post._id, comment._id),
postLink: getPostLink(post) postLink: getPostLink(post)
}); });

View file

@ -5,34 +5,49 @@ Meteor.startup(function () {
Router.route('/feed.xml', function () { Router.route('/feed.xml', function () {
this.response.write(servePostRSS('new', 'feed.xml')); this.response.write(servePostRSS('new', 'feed.xml'));
this.response.end(); this.response.end();
}, {where: 'server'}); }, {
name: 'feed',
where: 'server'
});
// New Post RSS // New Post RSS
Router.route('/rss/posts/new.xml', function () { Router.route('/rss/posts/new.xml', function () {
this.response.write(servePostRSS('top', 'rss/posts/new.xml')); this.response.write(servePostRSS('top', 'rss/posts/new.xml'));
this.response.end(); this.response.end();
}, {where: 'server'}); }, {
name: 'rss_posts_new',
where: 'server'
});
// Top Post RSS // Top Post RSS
Router.route('/rss/posts/top.xml', function () { Router.route('/rss/posts/top.xml', function () {
this.response.write(servePostRSS('top', 'rss/posts/top.xml')); this.response.write(servePostRSS('top', 'rss/posts/top.xml'));
this.response.end(); this.response.end();
}, {where: 'server'}); }, {
name: 'rss_posts_top',
where: 'server'
});
// Best Post RSS // Best Post RSS
Router.route('/rss/posts/best.xml', function () { Router.route('/rss/posts/best.xml', function () {
this.response.write(servePostRSS('best', 'rss/posts/best.xml')); this.response.write(servePostRSS('best', 'rss/posts/best.xml'));
this.response.end(); this.response.end();
}, {where: 'server'}); }, {
name: 'rss_posts_best',
where: 'server'
});
// Comment RSS // Comment RSS
Router.route('/rss/comments.xml', function() { Router.route('/rss/comments.xml', function() {
this.response.write(serveCommentRSS()); this.response.write(serveCommentRSS());
this.response.end(); this.response.end();
}, {where: 'server'}); }, {
name: 'rss_comments',
where: 'server'
});
}); });

View file

@ -4,9 +4,9 @@ var getMeta = function(url) {
return { return {
title: getSetting('title'), title: getSetting('title'),
description: getSetting('tagline'), description: getSetting('tagline'),
feed_url: Meteor.absoluteUrl()+url, feed_url: Meteor.absoluteUrl(url),
site_url: Meteor.absoluteUrl(), site_url: Meteor.absoluteUrl(),
image_url: Meteor.absoluteUrl()+'img/favicon.png', image_url: Meteor.absoluteUrl('img/favicon.png'),
}; };
}; };
@ -32,7 +32,7 @@ servePostRSS = function(view, url) {
}; };
serveCommentRSS = function() { serveCommentRSS = function() {
var feed = new RSS(getMeta('rss/comments.xml')); var feed = new RSS(getMeta(Router.routes['rss_comments'].path()));
Comments.find({isDeleted: {$ne: true}}, {sort: {postedAt: -1}, limit: 20}).forEach(function(comment) { Comments.find({isDeleted: {$ne: true}}, {sort: {postedAt: -1}, limit: 20}).forEach(function(comment) {
post = Posts.findOne(comment.postId); post = Posts.findOne(comment.postId);

View file

@ -30,7 +30,7 @@ Meteor.startup(function () {
// if search field is empty, just do nothing and show an empty template // if search field is empty, just do nothing and show an empty template
$search.addClass('empty'); $search.addClass('empty');
Session.set('searchQuery', ''); Session.set('searchQuery', '');
Router.go('/search', null, {replaceState: true}); Router.go('search', null, {replaceState: true});
} else { } else {
$search.removeClass('empty'); $search.removeClass('empty');
// if search field is not empty, add a delay to avoid firing new searches for every keystroke // if search field is not empty, add a delay to avoid firing new searches for every keystroke
@ -38,10 +38,10 @@ Meteor.startup(function () {
Session.set('searchQuery', val); Session.set('searchQuery', val);
// Update the querystring. // Update the querystring.
var opts = {query: 'q=' + encodeURIComponent(val)}; var opts = {query: {q: val}};
// if we're already on the search page, do a replaceState. Otherwise, // if we're already on the search page, do a replaceState. Otherwise,
// just use the pushState default. // just use the pushState default.
if(getCurrentRoute().indexOf('/search') === 0) { if(Router.current().route.getName() === 'search') {
opts.replaceState = true; opts.replaceState = true;
} }
Router.go('search', null, opts); Router.go('search', null, opts);