【发布时间】:2014-12-25 17:06:23
【问题描述】:
Meteor 应用程序,新闻聚合器。 Mongo DB 结构:
{_id: channelId, title: channelTitle, pubDate: channelPubdate, items: [{title: newsTitle, desc: newsDescription, link: newsLink, pubDate: Date, clicks: 0}, {}, {} ...] }
{_id: channelId, title: channelTitle, pubDate: channelPubdate, items: [{title: newsTitle, desc: newsDescription, link: newsLink, pubDate: Date, clicks: 0}, {}, {} ...] }
...等
模板:
<template name="newsItems">
{{#each itemsList}}
{{#each this.items}}
<article class="news-body-custom" onmousedown="return false" onselectstart="return false">
<div class="news-content-left">
<h2 class="news-header-custom">{{this.title}}</h2>
<time class="time-custom">{{dateTranslate this.pubDate}}</time>
<p class="news-content-custom">{{{this.description}}}</p>
</div>
<div class="clicks">{{this.clicks}}</div>
</article>
{{/each}}
{{/each}}
事件处理程序:
Template.newsItems.events({
"click .news-body-custom": function(e) {
var iframe = document.getElementsByName("news-iframe")[0];
iframe.src = this.link;
var target = e.target;
while (target.className !== "news-body-custom") {
target = target.parentNode;
}
var clicks = target.getElementsByClassName("clicks")[0];
clicks.innerHTML = +clicks.innerHTML + 1;
debugger;
News.update({ _id: Session.get("channelId"), items: { $elemMatch: { link: this.link }}}, {$inc: {clicks: 1 }});
}
});
我有两个问题:
如何在此事件处理程序中访问 DOM 结构(模板)?我使用本机 JS 解决了这个问题 - 找到目标,然后找到与目标关联的元素。我认为这并不优雅。是否存在 Meteor 方式?
我需要在 Mongo 中通过增加此字段来保存点击次数。我怎么能做到这一点?我试试这个 News.update({ _id: Session.get("channelId"), items: { $elemMatch: { link: this.link }}}, { $inc: {clicks: 1 }});但这行不通。
谢谢你,新年快乐!
【问题讨论】: