我的应用程序使用 #each events
生成一个表。
每行有 4 张事件门票(每行代表一个事件),例如,当您点击第一张门票时,将执行以下操作:
Template.displayEventsTable.events({
"click .buyTicket1": function () {
buyEventTicket(1, this._id);
}
这是buyTicket
函数:
function buyEventTicket (whatNumber, whatEvent) {
var buyer = Meteor.user();
console.log(whatNumber); // works
console.log(whatEvent); // works
console.log(whatEvent.eventTicketPrice); // currently shows up as "undefined"
};
}
我遇到的问题是尝试获取 eventTicketPrice,它显示为未定义。尽管通读了 Meteor 和 HandleBars 文档,但仍未亮起可能导致此问题的原因。
事件 ID 不会为它自己的字段之一返回正确的值似乎不合逻辑。
我错过了什么?
编辑 1
HTML 模板
<template name="myEvents">
<h2>Events</h3>
<table>
<tr class="first-row">
<th>Ticket 1</th>
<th>Ticket 2</th>
<th>Ticket 3</th>
<th>Ticket 4</th>
</tr>
{{#each events}}
{{#if eventsTypeA 1}}
{{> displayEventsTable}}
{{/if}}
{{/each}}
</table>
</div>
</div>
</template>
事件表模板
<template name="displayEventsTable">
<tr>
<td><button class="buyTicket1 button-blue">Buy #1</button></td>
<td><button class="buyTicket2 button-blue">Buy #2</button></td>
<td><button class="buyTicket3 button-blue">Buy #3</button></td>
<td><button class="buyTicket4 button-blue">Buy #4</button></td>
</tr>
</template>
编辑 2
_id: "YQXvUoBGKvhPObzFw"
createdAt: Sat Jan 02 2016 22:47:35 GMT+0100 (CET)
eventStatus: "open"
eventTicketPrice: 30
eventTickets: Array[4]
__proto__: Object
最佳答案
您可以在 {{> displayEventsTable}}
之前使用 {{#with this}}
,然后使用 {{/with}}
关闭> 像这样:
<template name="myEvents">
<h2>Events</h3>
<table>
<tr class="first-row">
<th>Ticket 1</th>
<th>Ticket 2</th>
<th>Ticket 3</th>
<th>Ticket 4</th>
</tr>
{{#each events}}
{{#if eventsTypeA 1}}
{{#with this}}
{{> displayEventsTable}}
{{/with}}
{{/if}}
{{/each}}
</table>
</div>
</div>
</template>
关于javascript - 选择从数据上下文传递的 MongoDB 中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35820602/