选择 MongoDB 中从数据上下文传递的字段
Selecting a field in MongoDB that was passed from a data context
我的应用使用 #each events
.
生成 table
每行有 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 不会 return 它自己的字段之一的正确值似乎不合逻辑。
我错过了什么?
编辑 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>
事件Table模板
<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>
您只是将 this._id
传递给您的 buyEventTicket
函数,而不是整个对象。只需传入 this
即可访问该对象的其他属性:
Template.displayEventsTable.events({
"click .buyTicket1": function () {
buyEventTicket(1,this);
});
我的应用使用 #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 不会 return 它自己的字段之一的正确值似乎不合逻辑。
我错过了什么?
编辑 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>
事件Table模板
<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>
您只是将 this._id
传递给您的 buyEventTicket
函数,而不是整个对象。只需传入 this
即可访问该对象的其他属性:
Template.displayEventsTable.events({
"click .buyTicket1": function () {
buyEventTicket(1,this);
});