[英]Cutting collection field short in Meteor to make an ingress
In my Meteor project's home page where I am showing the latest post, I would like to show only the first 140 characters of the post and then a Read more
-button. 在我正在显示最新帖子的Meteor项目的主页上,我只想显示帖子的前140个字符,然后显示“ Read more
按钮。
Say I have 说我有
Posts = new Meteor.Collection('posts');
And Posts
has a .body
-field which is where I store the main text. Posts
有一个.body
-field,这是我存储正文的地方。 Then in the template helpers file I guess is where I have to do surgery: 然后在模板帮助器文件中,我想这是我必须做手术的地方:
Template.home.helpers({
var posts = Posts.find();
//surgery on all body fields in posts
posts: posts
});
I figure I have to perhaps use UnderscoreJS' _.each()
and the jQuery's .substring()
, but how I am going to do it and put it all together is where the problem is. 我认为我可能必须使用_.each()
和jQuery的.substring()
,但是问题出在哪里。
You could solve this entirely with CSS3 and (pure) JavaScript. 您可以使用CSS3和(纯)JavaScript完全解决此问题。
CSS3 introduces a new attribute text-overflow
, as described here: https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow CSS3引入了新的text-overflow
属性,如下所述: https : //developer.mozilla.org/en-US/docs/Web/CSS/text-overflow
You could use that in conjunction with a limited width/height of a div
or span
and then follow that with your "read more" link. 您可以将其与div
或span
的有限宽度/高度结合使用,然后在“ read more”链接之后进行操作。 The link would need to do nothing more than change the width/height (perhaps with an animation if you want). 该链接只需要更改宽度/高度即可(如果需要,可以使用动画)。
Alternatively, if you really want to use meteor for that, then you could add a transform
function to your find: 另外,如果您真的想为此使用流星,则可以在您的查找中添加一个transform
函数:
var posts = Posts.find({}, {transform: function(post) {
post.short = post.body.slice(0,140);
return post;
});
And then just use {{ short }}
in place of {{ body }}
. 然后只需使用{{ short }}
代替{{ body }}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.