【发布时间】:2015-05-01 19:09:41
【问题描述】:
我有一个关于如何在我的 JS 和 HTML 周围传递 Mongo 文档的问题。我在下面有一个示例,我试图让我的弹出框显示悬停在上面的缩略图的名称。当有人点击缩略图时,会弹出一个显示图片以及缩略图描述的模式。问题是,当我尝试将 this.name 元素传递给弹出框时,它会显示我悬停在所有缩略图上的第一个缩略图的名称,但它不使用所有模式上第一个缩略图的信息(它实际上是动态的)。我想知道我做错了什么,我发现的唯一解决方法是将 {{name}} 标记放在我的实际 html 中作为缩略图的数据内容。
HTML
<template name="gallery">
<div class="col-xs-6 col-sm-4 col-md-2">
<a href="#" class="thumbnail" data-content='{{name}}'>
<img src="{{img}}" alt="...">
<div class="caption">
<h5><center>{{name}}</center></h5>
</div>
</a>
</div>
</template>
JS
Gallerys = new Mongo.Collection("gallerys");
if (Meteor.isClient) {
// This code only runs on the client
Template.body.helpers({
gallerys: function () {
return Gallerys.find({}, {sort: {createdAt :-1}});
}
});
Template.body.events({
"mouseover a.thumbnail" :function(event, template){
$('a.thumbnail').popover({
trigger: "hover",
placement: "bottom",
})
},
"click a.thumbnail": function(event, template) {
event.preventDefault();
bootbox.dialog({
title: '<center><h2>'+ this.name.toUpperCase() +'</h2></center>',
message: '<div class="row"><div class="col-md-3"><img class="img-rounded" height="200" width="200" src="' + this.img + '"></div><div class="col-md-9"><h4>Description</h4>"'+ this.description +'"</div></div>',
size: 'large',
buttons: {
play:{
label:'PLAY SAMPLE <span class="glyphicon glyphicon-play"></span>',
className:"btn-success pull-left modalbtn"
},
download: {
label:'DOWNLOAD <span class="glyphicon glyphicon-download-alt"></span>',
className:"btn-primary modalbtn",
callback: function() {}
}
},
onEscape: function() {},
});
}
});
}
【问题讨论】:
-
您不应该在
Template.gallery而不是Template.body上进行活动吗? -
是的,现在的代码组织得不是很好,但我只是想在组织所有内容之前先了解一下概念。到目前为止,我认为更改为 Template.gallery 不会有太大的作用。我想知道如何传递这些元素。
标签: javascript mongodb meteor modal-dialog popover