Add extra field support to posts_list_compact template

This commit is contained in:
Sacha Greif 2015-05-08 09:20:22 +09:00
parent 88be07cbe5
commit 3de1cf87eb
10 changed files with 77 additions and 14 deletions

View file

@ -0,0 +1,5 @@
{
"postedAt": "Posted At",
"upvotedAt": "Upvoted At",
"downvotedAt": "Downvoted At"
}

View file

@ -0,0 +1,5 @@
{
"postedAt": "Posté le",
"upvotedAt": "Upvoté le",
"downvotedAt": "Downvoté le"
}

View file

@ -23,6 +23,9 @@ hasMorePosts (Boolean)
loadMoreHandler (Function)
| What to do when the user clicks "load more"
postsField (Object)
| An object cont
-->
<template name="posts_list_compact">
@ -30,13 +33,13 @@ loadMoreHandler (Function)
<thead>
<tr>
<td>Post</td>
<td>Downvoted At</td>
<td>{{fieldLabel}}</td>
</tr>
</thead>
{{#each postsCursor}}
<tr>
<td><a href="{{pathFor route='post_page' _id=_id}}">{{title}}</a></td>
<td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td>
<td>{{fieldValue}}</td>
</tr>
{{/each}}
{{#if hasMorePosts}}

View file

@ -1,5 +1,5 @@
Template.posts_list_compact.helpers({
postsCursor : function () {
postsCursor: function () {
if (this.postsCursor) { // not sure why this should ever be undefined, but it can apparently
var posts = this.postsCursor.map(function (post, index) {
post.rank = index;
@ -9,5 +9,25 @@ Template.posts_list_compact.helpers({
} else {
console.log('postsCursor not defined');
}
},
fieldLabel: function () {
return this.controllerOptions.fieldLabel;
},
fieldValue: function () {
var controllerOptions = Template.parentData(3).data.controllerOptions;
return controllerOptions.fieldValue(this);
}
});
});
Template.posts_list_compact.events({
'click .more-button': function (event) {
event.preventDefault();
if (this.controllerInstance) {
// controller is a template
this.loadMoreHandler(this.controllerInstance);
} else {
// controller is router
this.loadMoreHandler();
}
}
});

View file

@ -1,3 +1,3 @@
<template name="postsListController">
{{> Template.dynamic template=template data=context}}
{{> Template.dynamic template=template data=data}}
</template>

View file

@ -62,7 +62,9 @@ Template.postsListController.helpers({
template: function () {
return !!this.template? this.template: "posts_list";
},
context: function () {
data: function () {
var context = this;
var instance = Template.instance();
@ -73,7 +75,7 @@ Template.postsListController.helpers({
var parameters = Posts.getSubParams(terms);
var postsCursor = Posts.find(parameters.find, parameters.options);
var context = {
var data = {
// posts cursor
postsCursor: postsCursor,
@ -96,10 +98,12 @@ Template.postsListController.helpers({
},
// the current instance
controllerInstance: instance
controllerInstance: instance,
controllerOptions: context.options // pass any options on to the template
};
return context;
return data;
}
});

View file

@ -1,11 +1,12 @@
// object containing post list view parameters
/**
* Post views are filters used for subscribing to and viewing posts
* @namespace Posts.views
*/
Posts.views = {};
/**
* Add a module to a template zone
* @param {string} viewName - The name of the zone
* Add a post view
* @param {string} viewName - The name of the view
* @param {function} [viewFunction] - The function used to calculate query terms. Takes terms and baseParameters arguments
*/
Posts.views.register = function (viewName, viewFunction) {

View file

@ -3,6 +3,15 @@ Template.user_downvoted_posts.helpers({
var user = this;
return {
template: "posts_list_compact",
options: {
currentUser: user,
fieldLabel: i18n.t("downvotedAt"),
fieldValue: function (post) {
var user = this.currentUser;
var vote = _.findWhere(user.telescope.downvotedPosts, {itemId: post._id});
return moment(vote.votedAt).format("MM/DD/YYYY, HH:mm");
}
},
terms: {
view: 'userDownvotedPosts',
userId: user._id,

View file

@ -3,6 +3,13 @@ Template.user_posts.helpers({
var user = this;
return {
template: "posts_list_compact",
options: {
currentUser: user,
fieldLabel: i18n.t("postedAt"),
fieldValue: function (post) {
return moment(post.postedAt).format("MM/DD/YYYY, HH:mm");
}
},
terms: {
view: 'userPosts',
userId: user._id,

View file

@ -3,6 +3,15 @@ Template.user_upvoted_posts.helpers({
var user = this;
return {
template: "posts_list_compact",
options: {
currentUser: user,
fieldLabel: i18n.t("upvotedAt"),
fieldValue: function (post) {
var user = this.currentUser;
var vote = _.findWhere(user.telescope.upvotedPosts, {itemId: post._id});
return moment(vote.votedAt).format("MM/DD/YYYY, HH:mm");
}
},
terms: {
view: 'userUpvotedPosts',
userId: user._id,