流星货币交换
问题描述:
我一直试图通过使用select
标记获取货币兑换更新并使用JQuery获取值。流星货币交换
我原来的计划是使用来自流星把手的{{#if}}
来做逻辑。在使用MongoDB切换字段时,当用户点击不同的选项时,它会自动切换货币字段。
我目前正在使用名为theara:moneyjs
的流星包。请点击here了解包装信息。
这里是我当前的代码,我有:
HTML
<template name="product_table">
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Currency
<select id="currency">
<option value="aud">AUS Dollar</option>
<option value="usd">US Dollar</option>
<option value="hkd">HK Dollar</option>
</select>
{{#each product}}
<tbody>
<tr>
<td>{{productName}}</td>
<td>{{productPrice}}</td>
<!-- {{#if getEXR}} Does not work, since is not a boolean value
<td>{{productPrice}}</td>
{{/if}} -->
</tr>
</tbody>
{{/each}}
</table>
</template>
的JavaScript
Template.product_table.helpers({
product: function() {
return Products.find({}, {sort:{createdAt:-1}});
},
getEXR: function() {
$(document).on('change', '#currency', function() {
var getCurrency = $("#currency option:selected").val();
if (getCurrency === "aud") {
//I am not quite sure, how grab specific field values from MongoDB
fx.convert(Products.find().productPrice()).from("USD").to("AUD");
}
else if (getCurrency === "usd") {
fx.convert(Products.find().productPrice()).from("USD").to("USD");
}
else if (getCurrency === "hkd") {
fx.convert(Products.find().productPrice()).from("USD").to("HKD");
}
}
)};
任何帮助,将不胜感激。
答
getEXR必须返回一个布尔值。 这不像在Javascript中,大火不要评价这一点。你所要做的EN帮手至极返回一个布尔
答
正如我在评论中提及了你应该跟随入门教程,但这里的关键组成部分是,你将不得不
- 确保你需要将数据从服务器发布到客户端
- 时创建此模板
- 使用模板事件你订阅的刊物
- 你可能会想设置一个反应VAR或会话时变种选择框变化和t母鸡在你的帮手中改变基于该反应性数据源的值。
- 使用模板片段#each内和辅助附加到(很容易让你访问当前产品放在你的助手)
答
您还需要从getEXR助手返回一个单独的值,而不是一个游标。再加上你的帮手没有返回任何东西!
你甚至不需要布尔值。改用Session变量(或反应性变量)。请看下图:
HTML
<template name="product_table">
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Currency
<select id="currency">
<option value="AUD">AUS Dollar</option>
<option value="USD">US Dollar</option>
<option value="HKD">HK Dollar</option>
</select>
{{#each product}}
{{> oneProduct}}
{{/each}}
</table>
</template>
<template name="oneProduct">
<tbody>
<tr>
<td>{{productName}}</td>
<td>{{productPrice}}</td>
<td>{{localPrice}}</td>
</tr>
</tbody>
</template>
的JavaScript
Template.product_table.helpers({
product: function() {
return Products.find({}, {sort:{createdAt:-1}});
}
)};
Template.product_table.events({
'change #currency': function(ev){
Session.set('currency') = $("#currency option:selected").val();
}
});
Template.oneProduct.helpers({
// with a nested template the data context (this) becomes a single product
localPrice: function() {
var currency = Session.get('currency');
return fx.convert(this.productPrice()).from("USD").to(currency);
}
)};
您可能还需要有一个默认的货币转换,并在模板onCreated处理程序初始化currency
会话变量。
你在发布你需要的数据吗?我假设你在客户端执行此操作时,需要从客户端数据存储(minimongo)获取数据,其次应该使用事件。你做过流星教程的介绍吗? HTTPS://www.meteor。com/tutorials/blaze/creating-an-app流星在方法上有很大的不同,你将会遇到这样的困难时刻。 – pushplaybang
对不起,延迟回复,是的,我正在发布和订阅我的数据。我的发布是在服务器端完成的,而订阅则在客户端完成,在同一个JS文件中完成。 –