Javascript 为数组中的#每个生成唯一变量,以便编写upvote/downvote应用程序
我对编码是全新的,所以请原谅我明显的无知。我的问题是:如何在MongoDB中为全局数组中的每个项目创建一个唯一的变量,以便统计投票数和投票数并进行相应排序。我是在Meteor框架下做这些的 这是我的密码:Javascript 为数组中的#每个生成唯一变量,以便编写upvote/downvote应用程序,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我对编码是全新的,所以请原谅我明显的无知。我的问题是:如何在MongoDB中为全局数组中的每个项目创建一个唯一的变量,以便统计投票数和投票数并进行相应排序。我是在Meteor框架下做这些的 这是我的密码: <template name="website_item"> <li> <a href="{{url}}">{{title}}</a> <p> {{description}} </p> <a
<template name="website_item">
<li>
<a href="{{url}}">{{title}}</a>
<p>
{{description}}
</p>
<a href="#" class="btn btn-default js-upvote">
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"> </span>
</a>
<a href="#" class="btn btn-default js-downvote">
<span class="glyphicon glyphicon-arrow-down" aria-hidden="true"> </span>
</a>
<p>
Votes: {{totalVotes}}
</p>
</li>
</template>
在collection.js中,我有:
Websites = new Mongo.Collection("websites");
import { Meteor } from 'meteor/meteor';
Meteor.startup(() => {
// code to run on server at startup
if (!Websites.findOne()){
console.log("No websites yet. Creating starter data.");
Websites.insert({
title:"Test site",
url:"http://www.test.com",
description:"This is a test.",
createdOn:new Date(),
totalVotes: 0
});
Websites.insert({
title:"Google",
url:"http://www.google.com",
description:"Popular search engine.",
createdOn:new Date(),
totalVotes: 0
});
}
});
在server.js中,我有:
Websites = new Mongo.Collection("websites");
import { Meteor } from 'meteor/meteor';
Meteor.startup(() => {
// code to run on server at startup
if (!Websites.findOne()){
console.log("No websites yet. Creating starter data.");
Websites.insert({
title:"Test site",
url:"http://www.test.com",
description:"This is a test.",
createdOn:new Date(),
totalVotes: 0
});
Websites.insert({
title:"Google",
url:"http://www.google.com",
description:"Popular search engine.",
createdOn:new Date(),
totalVotes: 0
});
}
});
我希望我的问题是全面和清楚的。我只是希望能够保存全局数组中每个项目的up和downvote计数,但现在只有一个变量对我没有任何作用
非常感谢 您缺少发布和订阅。这个想法是你的模板将订阅网站数据,然后服务器将发布你所要求的内容。您将编写一个助手,对发布的数据执行find(),并编写一个#each循环,循环遍历该数据 一旦你完成了这项工作,棘手的部分,也就是你要问的,是标记每个循环项,这样当你点击时,你可以在事件处理程序中唯一地识别它 让我们设置一个新模板(我正在这个文本框中编写所有代码,没有尝试过,所以请原谅拼写错误): 我假设您已经编写了一个发布,或者自动发布正在 然后编写帮助程序:
Template.Websites.helpers({
websites() {
return Websites.find({});
}
});
最后是事件侦听器,它可以识别单击了哪个项目:
Template.Websites.events({
'click .js-upvote': function(event, template) {
event.preventDefault();
if (event && event.currentTarget && event.currentTarget.dataset) {
let websiteId = event.currentTarget.dataset.id;
// now you can save the incremented votes for this website
}
}
});
关于你的TotalVoces变量,我想我明白你想做什么,现在你可以摆脱它了。使用我编写的代码,它会将每个网站的增量和减量保存到db中,并且因为您已订阅,所以您将获得更新的投票总数,并在模板中反应性地显示它
更新:
或者,访问_id而不将其写入DOM:
<template name="Websites">
{{#each website in websites}}
{{website.title}}
Votes: {{website.totalVotes}}
<button class="js-upvote">Vote Up</button>
{{/each}}
</template>
Template.Websites.events({
'click .js-upvote': function(event, template) {
event.preventDefault();
let websiteId = this._id;
// now you can save the incremented votes for this website
}
}
});
{{{#网站中的每个网站}
{{website.title}
投票:{{website.totalvows}
投票赞成
{{/每个}}
Template.Websites.events({
“click.js upvote”:函数(事件、模板){
event.preventDefault();
让websiteId=this.\u id;
//现在您可以保存此网站的增量投票
}
}
});
如果将数组转换为集合,则每个文档(行)都可以有一个唯一的\u id
。也可以使用$inc
而不是increment和$set
,因为在您的方法中,两次同时投票可能会给出错误的结果。谢谢Michel。这很有帮助!无需将\u id
写入DOMthis.\u id
在事件处理程序中,将为您提供所单击项目的\u id
。@MichelFloyd这对您100%有效吗?我认为我的团队遇到了这样的情况。_id不对,所以我们开始将其写入DOM。我没有看到它不起作用,但我经常将最低级别的对象放在自己的模板中,我发现它更适合事件处理。
<template name="Websites">
{{#each website in websites}}
{{website.title}}
Votes: {{website.totalVotes}}
<button class="js-upvote">Vote Up</button>
{{/each}}
</template>
Template.Websites.events({
'click .js-upvote': function(event, template) {
event.preventDefault();
let websiteId = this._id;
// now you can save the incremented votes for this website
}
}
});